Yes, this is what I understand I should be able to do from reading up on log4perl, call get_logger.

I do understand I could pass the log handle to subscripts, modules, subroutines as a few people have suggested, but my impression was that log4perl should be able to work such that I don't have to pass the variable around.

In my subroutine I called get_logger() however when I ran this what I got was a new instance with the root logger being set up which means the log messages do not go to the logfiles created in my main script. I've just tried again by calling get_logger("logfile") in the subroutine exactly as it was called in my main script and it now seems to be doing what I want. I also ran 2 instances of the main script at the same time and got 2 instances of my log files as I want.

Update: I guess the question would be, is there a way to call get_logger in the subroutine such that I don't have to know what logger the main script called get_logger with?

If I have a 2nd script which could be called from the main script or could be called independently then would I do the Log::Log4perl->init_once so it would work under both scenarios? And again I would need to be calling get_logger("logfile") the same way for it to refer to the logger of the main script if being scheduled from the main script?

Is this what you were suggesting and does this approach make sense in terms of how the log4perl module works? I think my lack of object oriented coding skills is making it a bit difficult for me to really get how the logger instance and get_logger is working. I'm trying to fully understand so that I can set up a configuration and approach that will work generically for all our scripts.

Thanks for the help


In reply to Re^2: log4perl - accessing logger in subscripts/subroutines? by ck2021
in thread log4perl - accessing logger in subscripts/subroutines? by ck2021

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.