The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Su::Log - A simple Logger which filters output by log level and regexp of the target class name.

SYNOPSYS

  my $log = Su::Log->new;
  $log->info("info message.");

  # Set the log level to output.
  Su::Log->set_level("trace");
  $log->trace("trace message.");

  # Disable logging and nothing output.
  $log->off(__PACKAGE__);
  $log->info("info message.");

  # Clear the logging state.
  $log->clear(__PACKAGE__);

  # Enable logging.
  $log->on(__PACKAGE__);
  $log->info("info message.");

  # Clear the logging state.
  $log->clear(__PACKAGE__);

  # Set the logging target and log level.
  $log->on( 'Pkg::LogTarget', 'error' );

  # Set the logging target by regex.
  $log->on( qr/Pkg::.*/, 'error' );

  # Clear the logging state.
  $log->clear(qr/Pkg::.*/);

  # Output logs to the file.
  $log->log_handler('path/to/logfile');

DESCRIPTION

Su::Log is a simple Logger module. Su::Log has the following features.

Narrow down the output by log level.
Narrow down the logging target class.
Narrow down the output by customized log kind.
Customize the log handler function.

FUNCTIONS

on()

Add the passed module name to the list of the logging tareget. If the parameter is not passed, then set the whole class as logging target.

enable()

This method force enable the logging regardless of whether the logging of the target class is enabled or disabled.

Internally, this method set the $all_on flag on, and $all_off flag off. To clear this state, call the method Su::clear_all_flags.

off()

Disable the logging of the class which name is passed as a parameter.

 $log->off('Target::Class');

If the parameter is omitted, this effects only own instance.

 $log->off;
disable()

This method force disable the logging regardless of whether the logging of the target class is enabled or disabled.

Internally, ths method set the $all_off flag on, and $all_on flag off. To clear this state, call the method Su::clear_all_flags.

clear_all_flags()

Clear $all_on and $all_off flags that is set by Su::enable or Su::disable method.

clear()

If the parameter is passed, This method clear the state of the passed target that is set by the method on and off.

If the parameter is omitted, then clear all of the log settings.

tag_on()

Add the passed tag to the target tags list.

tag_off()

Remove the passed tag from the target tags list.

new()

Constructor.

 my $log = new Su::Log->new;
 my $log = new Su::Log->new($self);
 my $log = new Su::Log->new('PKG::TargetClass');

Instantiate the Logger class. The passed instance or the string of the module name is registered as a logging target class. If the parameter is omitted, then the caller is registered automatically.

is_target()

Determine whether the module is a logging target or not.

set_level()

Su::Log->set_level("trace");

Set the log level which effects instance scope. Default level is info;

set_global_log_level()

Su::Log->set_default_log_level("trace");

Set the log level. This setting effects as the package scope variable.

To clear the $global_log_level flag, pass undef to this method.

is_large_level()

Return whether the passed log level is larger than the current log level or not. If the second parameter is passed, then compare that value as the current log level.

trace()

Log the passed message as trace level.

info()

Log the passed message as info level.

warn()

Log the passed message as warn level.

error()

Log the passed message as error level.

crit()

Log the passed message as crit level.

debug()

Log the passed message as debug level.

log()

Log the message with the passed tag, if the passed tag is active.

  my $log = Su::Log->new($self);
  $log->log("some_tag","some message");
log_handler()

Set the passed method as the log handler of Su::Log.

  $log->log_handler(\&hndl);
  $log->info("info message");

  sub hndl{
    print(join 'custom log handler:', @_);
  }

  $log->log_handler(
    sub {
      my $level = shift;
      my $msg   = @_;
      print $F $level . join( ' ', @_ ) . "\n";
    }
  );

If the passed parameter is string, then automatically the handler is set to output log to the passed file name.