pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Hail the Monks of Perl!

I'm working with some objects in Moose. I have one object that has several others. Lets say I've got an Examiner who runs everything, a number of Tests that I run and a bunch of Actions that I take when the Tests fail. The Tests and Actions all belong to the Examiner, if you follow me. Each of these classes takes a Log::Dispatcher object. I want them all to have the same Log::Dispatcher object.

The idea would be that I can set up my Log::Dispatcher in the Examiner and have my logging calls work appropriately in all my Tests and Actions.

Right now, I have my Examiner generate the Log::Log::Dispatcher and each time I create a Test or Action I assign the Dispatcher like so:

# inside fictional Examiner class $self->add_test(MyTest->new( dispatcher => $self->dispatcher )); $self->dispatcher->notice("I just added a test"); ... #inside fictional MyTest class $self->dispatcher->warn("this example is silly!");

This feels cumbersome to me. I also feel a little funny that if I replace the Log::Dispatcher in the Examiner, the Tests and Actions will all still have the old one.

I'm hoping there is an established pattern to handle this and I'm going to slap myself in the forehead when one of you tells me about it.

Much Thanks!

--Pileofrogs

Replies are listed 'Best First'.
Re: Best way to share logging object among multiple objects?
by pemungkah (Priest) on Aug 06, 2010 at 05:06 UTC
    A singleton is the first thing that comes to mind - a class that always returns the same object each time its new() is called. So if you want a logger, you call Logger->new(), and always get the same object. Class::Singleton wraps the logic up for you.

    I am not familiar enough with this to point you at good documentation as to how to do it, but I can hear Ovid saying "create a logging role and give that to anything that needs to log". I really need to read his articles about this...basically you're using the extends ... with feature of Moose. See http://use.perl.org/~Ovid/journal/38586 for a solid intro to the concept.

      ++

      If I understand this correctly, I'd want to use a singleton and a role. Making my logger a singleton makes it so each object uses the same Log::Dispatch object. Making it a role makes it so I don't lose my logging capability down a rabbit hole of multiple inheritance in the event my code gets really complex.

      Can I even do that?

      Does that sound right? Did I totally miss the point?

      Thanks!

      --Pileofrogs

        Taking my advice with a chunk or rock salt the size of your head because I (at this late date) have still not learned Moose: I'd say yes, that's probably the simplest. If you use (say) Log::Log4perl, I think that the singleton issue's already taken care of, and all you need at that point is a role that provides the logging features of your choice. Having the role lets you (if you want to) abstract the logging so that you can say "please log this object" to the CanLog role, and the role takes care of the details (e.g., does this object have a detailed_description method I can call, or do I just Data::Dumper it?).
Re: Best way to share logging object among multiple objects?
by BrowserUk (Patriarch) on Aug 05, 2010 at 23:16 UTC

    You mean you want a single, global dispatcher?

Re: Best way to share logging object among multiple objects?
by bellaire (Hermit) on Aug 06, 2010 at 12:46 UTC
    Probably use Moose roles and have the Tests and Actions ask their own Examiner for the dispatcher in the role method, rather than having the dispatcher be contained by the tests and actions themselves. Assuming they know their examiners, something like:
    package Logging; #the role use Moose::Role; requires 'examiner'; # ask my examiner, rather than contain this directly. sub dispatcher { return $self->examiner->dispatcher(); } package MyTest; # the test class use Moose; with 'Logging'; #...later... $self->dispatcher->warn("this example might work?");