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' };

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

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

        $ dir /b shabba.pl $ perl shabba.pl $ dir /b A1.2012-10-29T10-49-10Z.log A2.2012-10-29T10-49-11Z.log Logfile.2012-10-29T10-49-10Z.log shabba.pl $ cat A1.2012-10-29T10-49-10Z.log 2012/10/29 03:49:10 INFO> shabba.pl:43 main::Main - (Groceries) Import +ant Info! 2012/10/29 03:49:10 INFO> shabba.pl:44 main::Main - (Groceries) Import +ant Info! THEN NEWLINE THEN IMPORTANTINFO $ cat A2.2012-10-29T10-49-11Z.log 2012/10/29 03:49:10 INFO> shabba.pl:48 main::Main - (Junk) UnImportant + Info! $ cat Logfile.2012-10-29T10-49-10Z.log 2012/10/29 03:49:10 shabba.pl 43> (Groceries) Important Info! 2012/10/29 03:49:10 shabba.pl 44> (Groceries) Important Info! THEN NEW +LINE THEN IMPORTANTINFO 2012/10/29 03:49:10 shabba.pl 48> (Junk) UnImportant Info! 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) YAM NEWLINE Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) YAM NEWLINE Twix mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) YAM NEWLINE Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) YAM NEWLINE Mars mjam

        testcase (creates 3 logfiles on each run)