The following situation has been bugging me for a long time, would like to seek your insights. Logging and Configuration are two things most of my modules/scripts use (I use Log::Log4perl and AppConfig.) This is ok for standalone scripts, where I simply do something like the following in the beginning:
use strict; use AppConfig; use Log::Log4perl qw(get_logger); my $config = AppConfig->new()->file("app.properties"); Log::Log4perl->init($config->get("log4perl")); # init log my $logger = get_logger(__PACKAGE__); # or some other name
in later part of the code, I can simply call $config and $logger.

The problem is when I try to do similar things to modules, who should construct/init the $config and $logger? It seems to be too much to write the above code to all the module, even too much as a base class. Should the caller of the module always pass in the $config and $logger? There is a pattern called "Inversion of Control" which, among other things, advocates that the creator is responsible for providing all environments, including configuration and logging. Does that make sense here? what do you do?


In reply to Inversion of control: logging and configuration by johnnywang

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.