|
NAMELog::Handler - Log messages to several outputs.SYNOPSISuse Log::Handler; my $log = Log::Handler->new(); $log->add( file => { filename => "file.log", maxlevel => "debug", minlevel => "warning", } ); $log->warning("message"); Or use Log::Handler; my $log = Log::Handler->new( screen => { log_to => "STDOUT", maxlevel => "debug", minlevel => "debug", message_layout => "%T [%L] %m (%C)", }, screen => { log_to => "STDOUT", maxlevel => "info", minlevel => "notice", }, screen => { log_to => "STDERR", maxlevel => "warning", minlevel => "emergency", }, ); Or use Log::Handler; my $log = Log::Handler->new(); $log->config( config => "logger.conf" ); # and maybe later $log->reload( config => "logger.conf" ); Or # create a application wide logger package MyApp; use Log::Handler; my $log = Log::Handler->create_logger("myapp"); $log->add(screen => { maxlevel => "info" }); $log->info("info message"); # get logger with get_logger() package MyApp::Admin; use Log::Handler; my $log = Log::Handler->get_logger("myapp"); $log->info("info message from MyApp::Admin"); DESCRIPTIONThe "Log::Handler" is a object oriented handler for logging, tracing and debugging. It is very easy to use and provides a simple interface for multiple output objects with lots of configuration parameters. You can easily filter the amount of logged information on a per-output base, define priorities, create patterns to format the messages and reload the complete logging machine.See the documentation for details. IMPORTANT NOTESNote that the default for option "newline" is now set to TRUE and newlines will be appended automatically to each message if no newline exists.A long time I thought about this serious change and have come to the decision to change it. The default for option "mode" from Log::Handler::Output::File is now "append" and not "excl" anymore. The methods "reload()" and "validate()" are new since version 0.62. I tested it with Screen.pm, File.pm and DBI.pm and it runs fine. If you find bugs then open a bug report please :-) LOG LEVELSThere are eigth levels available:7 debug 6 info 5 notice 4 warning, warn 3 error, err 2 critical, crit 1 alert 0 emergency, emerg "debug" is the highest and "emergency" is the lowest level. Level "debug" is the highest level because it basically says to log every peep. LOG LEVEL METHODSLevel methods
The call of a log level method is very simple: $log->info("Hello World! How are you?"); Or maybe: $log->info("Hello World!", "How are you?"); Both calls would log - if level INFO is active: Feb 01 12:56:31 [INFO] Hello World! How are you? is_* methods
These twelve methods could be very useful if you want to kwow if the current level would log the message. All methods returns TRUE if the current set of "minlevel" and "maxlevel" would log the message and FALSE if not. SPECIAL LOG METHODS
For a full list take a look into the documentation of Log::Handler::Levels. METHODSnew()Call "new()" to create a new log handler object.my $log = Log::Handler->new(); add()Call "add()" to add a new output object.The method expects 2 parts of options; the options for the handler and the options for the output module you want to use. The output modules got it's own documentation for all options. Example: use Log::Handler; my $log = Log::Handler->new(); $log->add( # Add "file output" file => { # handler options (see Log::Handler) timeformat => "%Y/%m/%d %H:%M:%S", message_layout => "%T [%L] %S: %m", maxlevel => "debug", minlevel => "emergency", die_on_errors => 1, debug_trace => 0, debug_mode => 2, debug_skip => 0, # file options (see Log::Handler::Output::File) filename => "file.log", filelock => 1, fileopen => 1, reopen => 1, autoflush => 1, permissions => "0660", utf8 => 1, } ); Take a look to Log::Handler::Examples for more examples. The following options are possible for the handler:
output()Call "output($alias)" to get the output object that you added with the option "alias".It's possible to access a output directly: $log->output($alias)->log(message => "booo"); For more information take a look to the option "alias". flush()Call "flush()" if you want to send flush to all outputs that can flush.Flush means to flush buffers and/or close and re-open outputs. If you want to send it only to some outputs you can pass the aliases. $log->flush(); # flush all $log->flush("foo", "bar"); # flush only foo and bar If option "die_on_errors" is set to 0 then you can intercept errors with: $log->flush or die $log->errstr; errstr()Call "errstr()" if you want to get the last error message. This is useful if you set "die_on_errors" to 0 and the handler wouldn't die on failed write operations.use Log::Handler; my $log = Log::Handler->new(); $log->add(file => { filename => "file.log", maxlevel => "info", die_on_errors => 0, }); $log->info("Hello World!") or die $log->errstr; Or unless ( $log->info("Hello World!") ) { $error_string = $log->errstr; # do something with $error_string } The exception is that the handler dies in any case if the call of "new()" or "add()" fails because on missing or wrong settings! config()With this method it's possible to load your output configuration from a file.$log->config(config => "file.conf"); Or $log->config(config => { file => [ { alias => "error_log", filename => "error.log", maxlevel => "warning", minlevel => "emerg", priority => 1 }, { alias => "common_log", filename => "common.log", maxlevel => "info", minlevel => "emerg", priority => 2 }, ], screen => { alias => "screen", maxlevel => "debug", minlevel => "emerg", log_to => "STDERR", }, }); The key "default" is used here to define default parameters for all file outputs. All other keys ("error_log", "common_log") are used as aliases. Take a look into the documentation of Log::Handler::Config for more information. reload()With the method "reload()" it's possible to reload the logging machine. Just pass the complete new configuration for all outputs, it works exaclty like "config()".At first you should know that it's highly recommended to set a alias for each output. If you don't set a alias then the logger doesn't know which output-objects to reload. If a output-objects doesn't have a alias then the objects will be removed and the new configuration will be added. Example: logger.conf <file> alias = debug filename = debug.log maxlevel = debug minlevel = emerg </file> <file> alias = common filename = common.log maxlevel = info minlevel = emerg </file> Load the configuration $log->config(config => "logger.conf"); Now change the configuration in logger.conf <file> alias = common filename = common.log maxlevel = notice minlevel = emerg </file> <sendmail> alias = sendmail from = bar@foo.example to = foo@bar.example subject = your subject </sendmail> What happends now... The file-output with the alias "debug" will be removed, the file-output with the alias "common" will be reloaded and the output with the alias "sendmail" will be added. If you don't want that output-objects will be removed because they were added internal, then you can set the option "remove_on_reload" to 0. Example: $log->config(config => "logger.conf"); $log->add( forward => { forward_to => \&my_func, remove_on_reload => 0, } ); The forward-output is not removed after a reload. validate()The method "validate()" expects the same arguments like "config()" and "reload()".Maybe you want to validate your options before you pass them to "config()" or "reload()". Example: my $log = Log::Handler->new(); $log->config( config => \%config ); # and maybe later if ( $log->validate( config => \%new_config ) ) { $log->reload( config => \%new_config ); } else { warn "unable to reload configuration"; warn $log->errstr; } set_pattern()With this option you can set your own placeholders. Example:$log->set_pattern("%X", "key_name", sub { "value" }); # or $log->set_pattern("%X", "key_name", "value"); Then you can use this pattern in your message layout: $log->add(file => { filename => "file.log", message_layout => "%X %m%N", }); Or use it with "message_pattern": sub func { my $m = shift; print "$m->{key_name} $m->{message}\n"; } $log->add(forward => { forward_to => \&func, message_pattern => "%X %m", }); Note: valid character for the key name are: "[%\w\-\.]+" set_level()With this method it's possible to change the log level at runtime.To change the log level it's necessary to use a alias - see option "alias". $log->set_level( $alias => { # option alias minlevel => $new_minlevel, maxlevel => $new_maxlevel, } ); set_default_param()With this methods it's possible to overwrite the default settings for new outputs.Normally you would do something like $log->add( file => { filename => "debug.log", maxlevel => "info", timeformat => "%b %d %Y %H:%M:%S", message_layout => "[%T] %L %P %t %m (%C)" } ); $log->add( file => { filename => "error.log", maxlevel => "error", timeformat => "%b %d %Y %H:%M:%S", message_layout => "[%T] %L %P %t %m (%C)" } ); Now you can simplify it with $log->set_default_param( timeformat => "%b %d %Y %H:%M:%S", message_layout => "[%T] %L %P %t %m (%C)" ); $logg->add( file => { filename => "debug.log", maxlevel => "info" } ); $log->add( file => { filename => "error.log", maxlevel => "error" } ); create_logger()"create_logger()" is the same like "new()" but it creates a global logger.my $log = Log::Handler->create_logger("myapp"); get_logger()With "get_logger()" it's possible to get a logger that was created with "create_logger()" or withuse Log::Handler "myapp"; Just call my $log = Log::Handler->get_logger("myapp"); If the logger does not exists then a new logger will be created and returned. exists_logger()With "exists_logger()" it's possible to check if a logger exists and it returns TRUE or FALSE.EXAMPLESLog::Handler::ExamplesBENCHMARKThe benchmark (examples/benchmark/benchmark.pl) runs on a Intel Core i7-920 with the following result:simple pattern output took : 1 wallclock secs ( 1.26 usr + 0.01 sys = 1.27 CPU) @ 78740.16/s (n=100000) default pattern output took : 2 wallclock secs ( 2.08 usr + 0.15 sys = 2.23 CPU) @ 44843.05/s (n=100000) complex pattern output took : 4 wallclock secs ( 3.22 usr + 0.23 sys = 3.45 CPU) @ 28985.51/s (n=100000) message pattern output took : 3 wallclock secs ( 2.72 usr + 0.16 sys = 2.88 CPU) @ 34722.22/s (n=100000) suppressed output took : 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1250000.00/s (n=100000) filtered caller output took : 2 wallclock secs ( 2.10 usr + 0.68 sys = 2.78 CPU) @ 35971.22/s (n=100000) suppressed caller output took : 1 wallclock secs ( 0.54 usr + 0.00 sys = 0.54 CPU) @ 185185.19/s (n=100000) filtered messages output took : 3 wallclock secs ( 2.62 usr + 0.08 sys = 2.70 CPU) @ 37037.04/s (n=100000) EXTENSIONSSend me a mail if you have questions.PREREQUISITESPrerequisites for all modules:Carp Data::Dumper Fcntl Params::Validate POSIX Time::HiRes Sys::Hostname UNIVERSAL Recommended modules: Config::General Config::Properties DBI IO::Socket Net::SMTP YAML Just for the test suite: File::Spec Test::More EXPORTSNo exports.REPORT BUGSPlease report all bugs to <jschulz.cpan(at)bloonix.de>.AUTHORJonny Schulz <jschulz.cpan(at)bloonix.de>.QUESTIONSDo you have any questions or ideas?MAIL: <jschulz.cpan(at)bloonix.de> IRC: irc.perl.org#perl If you send me a mail then add Log::Handler into the subject. COPYRIGHTCopyright (C) 2007-2009 by Jonny Schulz. All rights reserved.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |