Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

naming conventions for logs

by aufrank (Pilgrim)
on Aug 05, 2002 at 17:03 UTC ( #187747=perlquestion: print w/replies, xml ) Need Help??

aufrank has asked for the wisdom of the Perl Monks concerning the following question:

hey monks--

I'm working on the same database issue, and as I was looking through the logs that my script outputs, I realized I could probably be providing more useful information, especially in the names of the logs. right now I just do something like open CHANGE_LOG, "> ${table_name}-changes.log", and, if the log is one I want to hold on to, I usually end up renaming it so that it won't get overwritten. Appending might be nice, but the logs would get too big.

what conventions have you developed in naming your logs that give an easily readable but informative title to each separate file? code examples of open statements (i.e. open FOO_LOG, "> foo.log") would be appreciated.

thanks in advance,

Replies are listed 'Best First'.
Re: naming conventions for logs
by PodMaster (Abbot) on Aug 05, 2002 at 17:21 UTC
    I usually pay real close attention to logs when i'm writing/debugging something. My standard way of doing this has become
    BEGIN { use CGI::Carp qw( carpout ); carpout(\*LOG) if open(LOG,'>>'.__FILE__.'.err.log'); }
    Occassionally I throw in '>>'.__FILE__.'.'.$$.'.err.log'); or '>>'.__FILE__.'.'.time.'.err.log');, but very very rarely.

    Lately I tend to add "verbosity" levels to things I write so that in "production" environment, verbosity is off (only if hell breaks loose, does the error log get filled).

    I rarely "rotate" logs, as i've not written anything that gets massively used.

    Since my logging needs have been simple, every once in a while I check on me logs and if they're bigger then a 100k, i remove them (after looking closely, and quieting warnings, usually use of unitialized this or that, cause not all perl's have the "warnings" pragma)

    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: naming conventions for logs
by {NULE} (Hermit) on Aug 05, 2002 at 18:45 UTC
    Hey aufrank,

    If you don't mind a wee bit of overhead you can try running your logfile names through stftime. You can give your logfiles names like myprogram-%Y-%m-%d.log and as long as you perform your open-append each time when you go to write, and your files will be nicely broken into days for you.

    use POSIX qw/strftime/; # Main routine... my $logfile = "program-%Y-%m-%d.log"; &do_log($logfile, "This is the message I want to log"); sub do_log { my $logfile = shift; my $message = shift; open LOG, ">>".strftime($logfile, localtime); print LOG stftime("%H:%M:%S ", localtime); print LOG $message,"\n"; close LOG; }
    This will give you logfiles with names like program-2002-08-05.log (which will sort correctly) and each line will be prefixed by a HH:MM:SS timestamp. Then you could delete last month's logs with a rm *-2002-08-??.log or the like.

    Of course you may wish to make your logging routine a bit more sophisticated than this. (Some error checking would be nice, of course.)

    Good luck,

Re: naming conventions for logs
by traveler (Parson) on Aug 05, 2002 at 19:43 UTC
    For the most part I agree with {NULE}. I generally do something similar, but like podmaster I generally put in levels so log 3, $message only prints if the log level is 3 or greater. I have also created systems where I send the data to syslog (both on *nix and win32) at facility LOG_LOCAL0. This allows configuring the log behavior without editing the perl script. This is important for programs that run continuously. I even sometimes have the program respond to signals or other user input to chnage the log level at run time. There are multiple syslog modules on cpan, and activestate ships with (which requires a unless you hack it) for Windows.

    HTH, --traveler

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://187747]
Approved by ichimunki
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-12-03 08:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found