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

Hail, mighty Perlers!
Once again, this bumblin', stumblin', fumblin' (apologies to Chris Berman) Perl wannabe needs help.
I'm trying to use the Log4perl module, but there's something obviously wrong with my code. Here is my test script, run from my home directory:
#!perl; use strict; use warnings; use Log::Log4perl qw(get_logger); Log::Log4perl->init("log.conf"); my $logger = get_logger("logfile.log"); $logger->info("This will get logged\n"); $logger->debug("This will not\n"); print "The quick brown fox jumped over the lazy dogs\n";

Here is my log.conf (filed in my C:\Perl\site\lib\Log\Log4perl directory):
log4perl.rootLogger=INFO, LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename="c:report\logfile.log" log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=[%r] %F %L c - %m%n

When I try to run the test script, I get this error:
Cannot open config file 'log.conf' at C:/Perl/site/lib/Log/Log4perl/Config.pm line 592.

If it seems that I'm really dense, well, I won't argue that point. I've read Michael Schilli's tutorial, along with the implementaion doc that comes with Log4perl, but I'm still in a fog. Can someone (anyone?) please help?

Replies are listed 'Best First'.
Re: Log4perl config problem
by clwolfe (Scribe) on Jul 06, 2006 at 03:26 UTC
    Offhand, I would say that it can't find your config file. I would recommend using a full path, ie, change
    Log::Log4perl->init("log.conf"); to
    Log::Log4perl->init("C:/Perl/site/lib/Log/Log4perl/log.conf");

    Remember, if you don't give a full path, the path is interpreted relative to the currrent working directory of the running script - not the location of the perl modules you use.

Re: Log4perl config problem
by Khen1950fx (Canon) on Jul 06, 2006 at 04:16 UTC
    Hi, yburge. After I read your question, I looked at the docs for Log::Log4perl and read the Schilly piece. I admit that I've been a stumbling and a bumbling around perl like you---but whatelse is new? Most of the time, I feel like a blackhole; but there are times when I feel like a shooting star. So, hang in there. You can do it! I'd suggest going back to the Schilly piece and reread it. It seems clearer than the perldocs. Redefine your purpose and priorities. Then go back over your code and rewrite it. Hint: Look at the whole package...I looked at Schilli's examples, then I looked at yours. Here's what I came up with:
    #!/usr/bin/env perl use Log::Log4perl qw(:easy); Log::Log4perl->easy_init("log.conf"); logfile(); logfile("logfile"); sub logfile { my($what) =@_; my $logger = get_logger(); if (defined $what) { $logger->info("This will get logged in"); } else { $logger->debug("This will not"); } }
Re: Log4perl config problem
by ftumsh (Scribe) on Jul 06, 2006 at 09:50 UTC

    iirc,

    log4perl.rootLogger=INFO, LOGFILE
    means that it will only log INFO messages. Change it to
    log4perl.rootLogger=DEBUG, LOGFILE

    Have a look at the categories section for fine control over what goes where and when. For quick and simple scripts you can't beat

    use Log::Log4perl qw(:easy); Log::Log4perl->easy_init( { level => $debug ? $DEBUG : $INFO, file => ">>$log_file", layout => '[%-5p] [%d{yy-MM-dd hh:mm:ss.S +SSSSS}][%r] %m - line: %L%n' } );

    Which allows you to set the log level based on an option passed to the perl program.

    Then you can use things like

    INFO( 'Start' ); # Only use if not allowing STDOUT #LOGDIE( 'No destination directory defined!' ) unless $dest_dir; DEBUG( 'Dir: ', $dest_dir ) if $debug;
    John

    20060707 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips