Chapter 13. Logging
Logging is recording messages from my program so I can watch its progress or look at what happened later. This is much larger than recording warnings or errors from my program since I can also log messages when things are going well, instead of just when things don’t work. Along with configuration, logging is one of the features missing from most Perl applications, but it’s incredibly easy to add.
Recording Errors and Other Information
Web applications already have it made. They can send things to STDERR (through
whichever mechanism or interface the program might use), and they show up
in the web server error log.[48]Other programs have to work harder. In general, logging is
not as simple as opening a file, appending some information, and closing
the file. That might work if the program won’t run more than once at the
same time and definitely finishes before it will run again. For any other
case, it’s possible that two runs of the program running at the same time
will try to write to the same file. Output buffering and the race for
control of the output file mean that somebody is going to win, and not all
of the output may make it into the file.
Logging, however, doesn’t necessarily mean just adding something to a file. Maybe I want to shove the messages into a database, show them on the screen, send them to a system logger (such as syslogd), or more than one of those. I might want to send them directly to my email or pager. Indeed, if something is going wrong on ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access