Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

log file in Log4perl

by palette (Scribe)
on Jan 12, 2007 at 11:18 UTC ( [id://594350]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I wanted to create a logfile on the fly using Log4perl.
I am having the following lines in my code
log4perl.logger.ClassA = DEBUG, ClassA log4perl.appender.ClassA = Log::Log4perl::App +ender::File log4perl.appender.ClassA.filename = \ sub { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = l +ocaltime time; my $date=sprintf("%04d%02d%02d",1900+$year,$mon+1,$mday); my $log_file="/tmp/ClassA".$date.".log"; return $log_file; }


In the module the call to the conf file
BEGIN { Log::Log4perl::init_and_watch('/etc/log4perl.conf',5); $__PACKAGE__::mylog=Log::Log4perl->get_logger(); }
But when this module is called from the testscript able to see the following error.
the error reads something like this while executing the testscript.
# in classa.t at line 8. # Tried to use 'ClassA'. # Error: Can't evaluate 'sub {' (Missing right curly or square b +racket at (eval 136) line 2, at end of line # syntax error at (eval 136) line 2, at EOF # ) at /usr/local/lib/perl5/site_perl/5.8.5/Log/Log4perl/Config.pm lin +e 715.

Replies are listed 'Best First'.
Re: log file in Log4perl
by Melly (Chaplain) on Jan 12, 2007 at 12:12 UTC

    What happens if you try:

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = l +ocaltime time; my $date=sprintf("%04d%02d%02d",1900+$year,$mon+1,$mday); my $log_file="/tmp/ClassA".$date.".log"; log4perl.appender.ClassA.filename = $log_file;
    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk
Re: log file in Log4perl
by Anonymous Monk on Jan 12, 2007 at 11:39 UTC
    Aren't you missing \ at the end of every line of your sub?
      Hi,

      Can I know why a "\" is required at the end of every line of the sub. As per the CPAN description it seems that we have to send reference of the method. If wrong do correct me.

      Thanks

        In the piece of code you mentioned, you are not writing Perl code but a properties file (with lines "key = value") which is read by this statement:

        Log::Log4perl::init_and_watch('/etc/log4perl.conf',5);

        The value can be extended for many lines if they end in "\". There is more info in the FAQ within Log-Log4perl distribution.

        Because the Log4perl configuration file format requires each configuration item to be on a single line. You can continue a line onto the next line by ending it in a backslash, or you can just type it out as one really long line. If you review the documentation on CPAN again, you will notice that all of the examples use very short subs, and none of them span more than one line.

        The method I normally use for approaching this is something like...

        # config file log4perl.logger.ClassA = DEBUG, ClassA log4perl.appender.ClassA = Log::Log4perl::Appender::File log4perl.appender.ClassA.filename = sub { log_filename() } # in the main code use POSIX 'strftime'; sub log_filename { return strftime( "/tmp/ClassA%Y%m%d.log", localtime ); } Log::Log4perl::init_and_watch('/etc/log4perl.conf',5); my $mylog = Log::Log4perl->get_logger();

        We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found