NOCpulse::Log::Logger.3pm

Langue: en

Autres versions - même langue

Version: 2009-02-04 (fedora - 01/12/10)

Section: 3 (Bibliothèques de fonctions)

NAME

NOCpulse::Log::Logger - Fine-grain logging

SYNOPSIS

     use NOCpulse::Log::Logger;
 
     my $Log = NOCpulse::Log::Logger->new(__PACKAGE__);
 
     $Log->log(4, "Foo: ", $self->foo, "\n");
     $Log->dump(4, "Self: ", $self, "\n");
 
     if ($Log->loggable(9)) {
         $Log->log(9, "Entire database contents: ", $self->show_everything, "\n");
     }
 
     sub do_stuff {
        $Log->entering();
        ...
        $Log->exiting();
     }
 
 

DESCRIPTION

"Logger" provides methods for conditionally logging debug output. Each logger has its own level which can be set using the "LogManager" configuration methods. This provides fine-grained control over your output. By default configurations are read in from /etc/nocpulse/logging.ini. The keys are package names or prefixes, and the values are numeric levels. For example:
     NOCpulse::Dispatcher::Kernel=1
     NOCpulse::Dispatcher::Scheduler=0
 
 

EXAMPLE CONFIGURATION

Suppose you want to print all the level two messages from any subclass of "NOCpulse::Probe::DataSource", and the level four messages "NOCpulse::Probe::DataSource::SNMP". Between "Logger" and "LogManager" this is easy to set up:
     # Some setup code somewhere...
     LogManager->instance->configure(NOCpulse::Probe::DataSource => 1,
                                     NOCpulse::Probe::DataSource::SNMP => 4);
     # ...or /etc/nocpulse/logging.ini contains...
     # NOCpulse::Probe::DataSource=1
     # NOCpulse::Probe::DataSource::SNMP=4
 
     # In NOCpulse::Probe::DataSource::SNMP.pm:
     my $Log = NOCpulse::Log::Logger->new(__PACKAGE__);
 
     sub do_something {
         $Log->log(2, "Do something\n");
         $Log->log(4, "And here are all the details:", $self->details, "\n");
     }
 
 

METHODS

new($name [, $level, $show_method])
Creates a new logger. The name is typically "__PACKAGE__". You can specify the initial level number with $level; this overrides any "LogManager" configuration (see LogManager).

By default the name of the current method is logged with every message. You can override this at creation by specifying a false value for $show_method.

package_name()
Returns the name this logger was created with.
level([$level])
Returns the current output level for this logger, or sets it to a new value. The "log" method will not print anything if its level is greater than the current level.
show_method([$show])
Returns true if currently showing the current method in every log message, or resets this value if $show is present.
loggable($level)
Returns true if a call to "log()" at $level will actually print. This is useful to check before creating a large output message that may or may not be logged.
log($level, @args)
Logs a formatted message containg @args if $level is loggable. Formatting is described in Debug.
dump($level, $prefix, $ref, $suffix)
Dumps an object with Data::Dumper (surrounded by $prefix and $suffix) if the $level is loggable.
entering([$msg])
Prints the current method name followed by ``ENTER'' and the optional message if NOCpulse::Log::ENTER_EXIT_LEVEL is loggable.
exiting([$msg])
Prints the current method name followed by ``EXIT '' and the optional message if NOCpulse::Log::ENTER_EXIT_LEVEL is loggable.
print(@args)
Unconditionally prints the arguments.

CONSTANTS

ENTER_EXIT_LEVEL
The level at which the "entering" and "exiting" methods will print.