Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Can I from within a module access variables from the calling program?

by Anonymous Monk
on Oct 29, 2012 at 09:17 UTC ( [id://1001321]=note: print w/replies, xml ) Need Help??


in reply to Can I from within a module access variables from the calling program?

Q: What if I need dynamic values in a static Log4perl configuration file?

A: log4perl.appender.Logfile.filename = sub { logfile(); };

A: log4perl.appender.Logfile.filename = sub { MyHackySack->logfile(); };

A: log4perl.appender.Logfile.filename = sub { $Hallo::Ween::GLOBULON || 'punkinguts.log' };

Replies are listed 'Best First'.
Re^2: Can I from within a module access variables from the calling program?
by Anonymous Monk on Oct 29, 2012 at 09:49 UTC

    Also heed the security warning in the FAQ item I linked above, and circumvent the security issue with a custom appender like (the untested):

    package Log::Log4perl::Appender::HJOfilnamed; use parent qw[ Log::Log4perl::Appender::File ]; use POSIX(); sub new { my $class = shift; my %opts = @_; my $tim = POSIX::strftime('%Y-%m-%dT%H-%M-%SZ', gmtime); my $fname = join '.', grep defined, $opts{name}, $opts{appender}, +$tim, 'log'; $fname =~ s{::}{.}g; $fname =~ s{[^0-9a-zA-Z\-\+\.}{}g; $fname = $1 if $fname =~ /^(.*)$/ return $class->new( @_, filename => $fname ); }
    So  log4perl.appender.myFILE = Log::Log4perl::Appender::HJOfilnamed gets you a  MyApp.myFILE.2012-10-29T02-40-04Z.log

    You'd probably want to set other defaults to your liking :)( layout, mode, ... )

      Hi Anonymous Monk,

      Thank you for your answer, it seems to fix my issue, but I'm afraid I don't understand it quite well :(

      package Log::Log4perl::Appender::HJOfilnamed; use parent qw[ Log::Log4perl::Appender::File ];

      Are you creating and entire module juste for the purpose of logging ? I mean, that's a good idea, I must admit it never accured to me that I could do so... And the use parent, what is it doing precisely please ?

      If I understand quite well, you are creating an object to create a new name at every launch of the main script ? And you are calling it at the intialization of the logger ? Does this allows me to have the exact same logfile name for my script and its module ? (I mean, even if there's a delay of about a second between the two initializatons)

      And thanks for the way you are naming your file, basically I use the same, but I'll stick to my method, it seems rather complicated the way you do it, at least to complicate for me...

        And the use parent, what is it doing precisely please ?

        If you're really interested, read about it :) perldoc parent

        output

        testcase (creates 3 logfiles on each run)

        #!/usr/bin/perl -- use Path::Class; use constant THISFILE => file( __FILE__ )->absolute->stringify; use constant THISDIR => file( THISFILE )->dir->stringify; use strict; use warnings; use Log::Log4perl; chdir THISDIR or die Fudge( 'chdir', THISDIR ); Main( @ARGV ); exit( 0 ); sub Main { my $logconfig = q{; log4perl.logger.Groceries=DEBUG, A1 log4perl.appender.A1=Log::Log4perl::Appender::HJOfilnamed log4perl.appender.A1.mode=append log4perl.appender.A1.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A1.layout.ConversionPattern=%d %p> %F{1}:%L %M - (%c +) %m%n log4perl.logger.Junk=DEBUG, A2 log4perl.appender.A2=Log::Log4perl::Appender::HJOfilnamed log4perl.appender.A2.mode=append log4perl.appender.A2.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A2.layout.ConversionPattern=%d %p> %F{1}:%L %M - (%c +) %m%n log4perl.debug=true log4perl.b=DEBUG, A2 log4perl.category.Bar.Twix = DEBUG, Logfile log4perl.category.Bar.Mars = DEBUG, Logfile log4perl.appender.Logfile = Log::Log4perl::Appender::HJOfilna +med log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLay +out log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> (%c) + %m %n log4perl.rootLogger = DEBUG, Logfile }; Log::Log4perl::init( \$logconfig ); { my $log = Log::Log4perl::get_logger("Groceries"); $log->info("Important Info!"); $log->info("Important Info! THEN NEWLINE\nTHEN IMPORTANTINFO") +; } { my $log = Log::Log4perl::get_logger("Junk"); $log->info("UnImportant Info!"); } Bar::Twix::eat(); Bar::Mars::eat(); } BEGIN{ $INC{'Log/Log4perl/Appender/HJOfilnamed.pm'} = __FILE__; package Log::Log4perl::Appender::HJOfilnamed; use parent qw[ Log::Log4perl::Appender::File ]; use POSIX(); sub new { my $class = shift; my %opts = @_; my $tim = POSIX::strftime('%Y-%m-%dT%H-%M-%SZ', gmtime); my $fname = join '.', grep defined, $opts{name}, $opts{appende +r}, $tim, 'log'; $fname =~ s{::}{.}g; $fname =~ s{[^0-9a-zA-Z\-\+\.]}{}g; $fname = $1 if $fname =~ /^(.*)$/; return $class->SUPER::new( @_, filename => $fname ); } } sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; } BEGIN { package Bar::Twix; use Log::Log4perl qw(:easy); sub eat { DEBUG("Twix mjam");INFO("YAM NEWLINE\nTwix mjam"); } package Bar::Mars; use Log::Log4perl qw(:easy); sub eat { INFO("Mars mjam");INFO("YAM NEWLINE\nMars mjam"); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1001321]
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: (5)
As of 2024-03-28 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found