Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How to write to log files?

by ajt (Prior)
on Nov 20, 2001 at 15:57 UTC ( [id://126501]=perlquestion: print w/replies, xml ) Need Help??

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

Like many diligent programmers, I like to log what is going on, so when things go wrong I can try and debug the problem. Alas I seem to end up cutting and pasting a simple log routine from program to program, and I end up with log files scattered all over the place. I work on both NT and Linux, so I can't easily write code that logs to the various system logs as this reduces portability.

Most of my applications are CGI, and during debugging I like to have lots of activity logged, and then reduce this to just enough for production. At the moment, I have long multi-line enteries that are easy to read by eye rather than machine. However in production I think a more machine readable format will be in order.

  • What logging modules have people used?
  • Would you use them/it again?
  • If you found that the CPAN modules weren't ideal, and you wrote your own, what did you put in your log: what format did you use?

I didn't find anything here with super search, but CPAN is full of modules! It's hard to decided where to begin, there are so many. Plus there is the added noise of the zillions of Apache log parsing modules!

Many humble thanks in advance.

Replies are listed 'Best First'.
Re: How to write to log files?
by miyagawa (Chaplain) on Nov 20, 2001 at 16:05 UTC
Re: How to write to log files?
by jepri (Parson) on Nov 20, 2001 at 18:30 UTC
    package mylog; use strict; use Data::Dumper; open LOGFILE, ">log.txt"; sub log { print LOGFILE, Dumper @_; } sub END { close LOGFILE; } #now in your main proggie &mylog::log("This is a logged message");

    There are many better variations, but this is a good first module.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      One small error: there shouldn't be a comma after print LOGFILE. The line should read:
      print LOGFILE Dumper @_;
Re: How to write to log files?
by drifter (Scribe) on Nov 20, 2001 at 20:01 UTC
    Well, "Sys::Syslog" hasn't failed me yet, and it's there by default on newer perls (i think 5.6+), so I can be sure I don't need additional packages when transferring a script, as I'm still not so familiar with dependencies.
Re: How to write to log files?
by dug (Chaplain) on Nov 20, 2001 at 21:01 UTC
    If you are lucky enough to be running Apache under both Linux and Win32,Apache::Log is a good interface Apache's logging mechanism, and works on both platforms. If that doesn'nt work, you could wrap an if statement like the one below around all of your critical logs. It will keep them in a central location to the system that the sysadmins are always diligently monitoring.
    if (`uname` =~ /Linux/) { # do Sys::Syslog stuff here } elseif (`set os` =~ /windows/i) { # do Win32::EventLog stuff here } else { die "hmmm, where do I log\n" }
    Of course, it would be smart to wrap this into Jepri's mylog::log example to save some keystrokes.
Re: How to write to log files?
by jlongino (Parson) on Nov 20, 2001 at 23:13 UTC
    I've found that my logging methods vary greatly depending on the type of application I'm writing and whether it is in the debugging or production phases.

    I've never really considered adding to the system logs because they tend to fill up rapidly, get aged, and deleted. Many times system logs are also located on partitions that are relatively short on space, so I don't want to aggravate that problem either.

    I prefer creating application specific log files that are human readable at all times, so I try to use messages that are visually distinctive for the various types of logged messages. As I'm debugging I comment out debug statements from areas as I complete them so the amount of information logged decreases greatly as I near the end of a project. The parts left are usually checkpoints that indicate success or failure of a given task.

    When debugging some web applications sometimes I find it more helpful to bypass file logging and generate simple javascript alerts:

    print "<script language='javascript'>alert('Exiting sub XXXXXXXXXX.')< +/script>";
    For more extensive web debugging info I use a html sub that is passed a "Continue" value that can be used to halt execution if necessary:
    sub Notify { my ($Text, $continue, $green) = @ARG; my $bgcolor = '#FC8C8C'; if ($green) { $bgcolor = '#BBFFBB'; } print <<"-100-"; <br><br><br><br> <table align=center BGCOLOR="$bgcolor" cellpadding=15 border=3> <tr><td align=center><font size=5 face=Arial> <b>$Text</b></font></td></tr> </table><br><br><br><br><br> -100- if (!$continue) { ## fatal message print <<"-200-"; <table border=0 width=100%> <tr><td align=center><font size=4 face=Arial color="blue"> <b>Contact Whomever Necessary at 999-9999 (Mon-Fri, 8am-5pm +) for questions regarding use of the this application.</b> </font></td></tr> </table> </center> </body> </html> -200- exit; } print <<"-300-"; </center> </body> </html> -300- }
    Yes, I realize that I could take better advantage of CGI.pm, and I probably will rewrite this sub to do so when I undertake my next Web application (which may not be for several months).

    --Jim

Re: How to write to log files?
by tstock (Curate) on Nov 21, 2001 at 01:31 UTC
    I've used Log::Agent , and I like the ability to change drivers:

    Log::Agent::Driver::File
    Log::Agent::Driver::Syslog
    Log::Agent::Driver::Silent

    Tiago
Re: How to write to log files?
by fuzzysteve (Beadle) on Nov 21, 2001 at 05:56 UTC
    I use Unix::syslog myself. Has the advantage of direct writing to the syslog, so it gets forwarded to a remote host if your machine is configured to do so. Works well and is easy to use. Its become part of my standard install.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found