[Pas-dev] Suggestion for logging
Status: Beta
Brought to you by:
mortis
From: Kyle R. B. <mo...@vo...> - 2003-06-12 23:06:05
|
In the Java projects I've been involved with, we've used Jakarta's Log4j. Probably the most important features of the logging library are that you can tweak the logging level or the destination(s) from a configuration file. The logging level (fatal, error, warn, info or debug) can be set across the entire project, on a per package or a per-object basis. For example, if you were trying to watch a constrained set of modules, you could set up a configuration like: log4perl.logger=ERROR, file log4perl.logger.Org.Bgw=INFO, screen, file log4perl.logger.Org.Bgw.Pas.Page=DEBUG, screen, file log4perl.logger.Org.Bgw.Pas.Response=ERROR, file log4perl.logger.file=Log::Dispatch::File log4perl.logger.file.filename=logs/pas.log log4perl.logger.file.layout=Log::Log4perl::Layout::PatternLayout log4perl.logger.file.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n log4perl.logger.screen=Log::Dispatch::Screen log4perl.logger.screen.stderr=0 log4perl.logger.screen.layout=Log::Log4perl::Layout::SimpleLayout The first section sets up the default level so only ERROR, or FATAL get logged. As a default for all the objects and sub-packages of Org::Bgw, it sets up a default of INFO or more severe. For Page it turns the log level all the way up to DEBUG, while turning it back down to ERROR for Response. To make the support easier, since perl can detect the calling package, we could centralize the configuraiton and obtaining of the logger to a single module: ### package Org::Bgw::Log; ... use Log::Log4perl; Log::Log4perl->init($ENV{'PAS_BASE'}."/conf/log4perl.conf"); # get a Log4perl log object based on the caller's package name sub getLogger { Log::Log4perl::get_logger( (caller)[0] ); } Then in any random module: ### package Org::Bgw::Foo; ... my $log = Org::Bgw::Log::getLogger; sub somethingComplicated { my($self) = @_; # testing log->is_debug is an optimization so that Dumper isn't # called (since it could be expensive) unless the log level is set # to debug -- unless the level is debug, the arguments aren't # computed, and the log statement isn't called. if( $log->is_debug() ) { $log->debug("somethingComplicated, self = ", Dumper($self) ); } eval { $log->info("Entering log running loop..."); while(...) { } $log->info("major processing completed"); }; if($@) { $log->fatal("Woah, bad error in main processing, aborting: $@"); $self->throw($@); } reutrn 1; } This article gives a good overview: http://www.perl.com/pub/a/2002/09/11/log4perl.html This is the main project site: http://log4perl.sourceforge.net/ It's also available from CPAN (http://cpan.org/modules/by-module/Log/). The project comes with a few appenders: Screen, File and DBI. It can also make use of Log::Dispatch appenders, which include: Email, FileRotate (good for keeping log files from filling up all free space), Tk and Syslog. One thing that could be done for production would be to set up fatal errors to go to a File appender, and an Email appender where the Email appender can be used for easy notification of catastrophic errors. The functionality is good, the downside is that this introduces another dependency. I had thought that Log::Dispatch::Config had these features, but it turns out that it doens't really. What do you guys think about switching? It should be an easy change. Kyle -- Health Market Science, Inc. 625 Ridge Pike, Bldg. E, Ste. 400 Conshohocken, PA 19428 Office: (610) 940-4002 x116 Fax: (610) 940-4003 Email: kb...@hm... Web: http://www.hmsonline.com/ ----- End forwarded message ----- -- ------------------------------------------------------------------------------ Wisdom and Compassion are inseparable. -- Christmas Humphreys mo...@vo... http://www.voicenet.com/~mortis ------------------------------------------------------------------------------ |