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

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 ( #1001329=note: print w/ replies, xml ) Need Help??


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

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, ... )


Comment on Re^2: Can I from within a module access variables from the calling program?
Select or Download Code
Re^3: Can I from within a module access variables from the calling program?
by HJO (Acolyte) on Oct 29, 2012 at 10:31 UTC

    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...

        Thanks for the perldoc link, I've read it and will maybe hopefully understand its implications someday...

        About your code, I think I got it (not quite sure ^^'), but it's not really an anwser to my issue... I'm looking at a way to have the same logfile name for my script and module while I'm running them... And I'm afraid that as you had, if there's a 1 second delay, the two files would not have the same name...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-07-31 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls