in reply to Log::Log4perl and singleton design issue

I have a related un-comfortable feeling when using L4p. I use log4j for my java projects, same feeling there. When coding my packages/objects, I always use a package-wide variable, as suggested by joost in his post above (Re: Log::Log4perl and singleton design issue). I agree with him that seems to be the right thing, and works fine for me. My problem is that whenever I write a script that uses those packages, I need to remember to always call Log::Log4perl->init("file.conf"). This drives me crazy since I can't easily write a commandline one-liner, or a simple test script. I've run into this numerous times, have to figure out where the file.conf is, etc.. Is there a way to avoid this? I'm not even sure what a correct solution is, but if I constantly run into this, something is not right.
  • Comment on Re: Log::Log4perl and singleton design issue

Replies are listed 'Best First'.
Re^2: Log::Log4perl and singleton design issue
by Tuppence (Pilgrim) on Dec 20, 2004 at 23:28 UTC

    I don't know what cool features of L4P you would be losing if you do this, so someone beat me with a cluestick if needed.

    Whenever I want to use a public module but I don't like it's semantics, I write a module to change it to be more friendly. Sometimes this is making a non-OO module behave in an OO fashion, sometimes it is merely changing the API of the object involved, and in a few cases it is turning an OO module into a non OO module. I think this is what you want here.

    sub log_it { Log::Log4Perl->logger('section') ->log(shift); }

    For instance, if you had a module that exported log_it, you could make sure it calls Log::Log4Perl->init('file.conf') (in BEGIN maybe?) and so make sure that it is always called. Boom, you get your oneliners back.

    Plus, you also get shorter call symantics - log_it('my_log_message') is a lot friendlier then Log::Log4Perl->logger('section')->log('my_log_message), but then of course it is the code that is actually run by log_it and so does the same thing with less typing.