[sf-perl] Logging: per-object, per-package, kinda-global?

Jeffrey Ryan Thalhammer jeff at imaginative-software.com
Fri Apr 5 20:58:19 PDT 2013


On Apr 5, 2013, at 3:49 PM, George Hartzell wrote:

> We can all agree that globals are evil, right?  (you there in back,
> put your hand down, I'm trying to make a point...).
> 
> I'm looking at logging and am (once again) playing with a couple of
> the strategies that package on CPAN enable.

I had the same dilemma with Pinto.  I wanted to log stuff from any and all objects, but I didn't want a singleton or global logger.  So I ended up passing loggers to all the constructors.  I used a role (called "Loggable") to help make each class uniform.  But I never really liked having to pass that damn logger object all over the place.

I was using the logger as my chrome too -- to display diagnostic messages to the user.  But then I realized the user really has no interest in the messages from all those low-level classes.  There were only a handful of classes that could produce any kind of user-friendly message.

I still pass around the logger to each constructor, but only for a few classes now.  And instead of logging, the low-level classes just have a simple debug() function that spits messages to STDERR only if PINTO_DEBUG is set.  So I've sacrificed some (probably unnecessary) power & flexibility in exchange for simpler, cleaner code.

Your mileage may vary.

-Jeff 


More information about the SanFrancisco-pm mailing list