Here's a very quick and dirty example that I think uses parts of each technique you've shown. When new() is called on Library, we store a log as a class variable. It has a log() method that will return this log. In the case below, with Library::Foo, Library passes the log in. We could also have accessed it as $Library::log, or much more preferably $log = Library::log()->child('name');.

This way, each class has a copy of the top-level log which has had it's class name appended to the log name. Then, each sub inside of each package makes a copy of this class log object, which then in turn appends its name, so the log messages each reflect exactly where the log was used.

I've used numerous ways of doing things like this. This is but one of them.

use warnings; use strict; package Library::Foo; { my $log; sub new { my ($class, $logger) = @_; my $self = bless {}, $class; # set the class level log $log = $logger->child('Library::Foo'); # generate a local log per each sub my $log = $log->child('new'); $log->_0("creating new foo obj"); return $self; } } package Library; { use Logging::Simple; # set the class log my $log = Logging::Simple->new(name => 'Library'); sub new { my $self = bless {}, shift; # generate a child log per sub my $log = $log->child('new'); $log->_0('creating Library obj'); return $self; } sub log { # return the class level log obj return $log; } sub foo { my ($self) = @_; my $log = $log->child('foo'); $log->_0('about to crate lib::foo obj'); $self->{foo} = Library::Foo->new($self->log); } } package main; my $lib = Library->new; $lib->foo;

Output:

[2016-11-13 09:12:13.179][lvl 0][Library.new] creating Library obj [2016-11-13 09:12:13.180][lvl 0][Library.foo] about to crate lib::foo +obj [2016-11-13 09:12:13.180][lvl 0][Library.Library::Foo.new] creating ne +w foo obj

In reply to Re: Passing the logger's object by stevieb
in thread Passing the logger's object by v_melnik

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.