Log::Contextual::SimpleLogger - Super simple logger made for playing with Log::Contextual


Log-Contextual documentation Contained in the Log-Contextual distribution.

Index


Code Index:

NAME

Top

Log::Contextual::SimpleLogger - Super simple logger made for playing with Log::Contextual

SYNOPSIS

Top

 use Log::Contextual::SimpleLogger;
 use Log::Contextual qw( :log ),
   -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});

 log_info { 'program started' }; # no-op because info is not in levels
 sub foo {
   log_debug { 'entered foo' };
   ...
 }

DESCRIPTION

Top

This module is a simple logger made mostly for demonstration and initial experimentation with Log::Contextual. We recommend you use a real logger instead. For something more serious but not overly complicated, take a look at Log::Dispatchouli.

METHODS

Top

new

Arguments: Dict[ levels => Optional[ArrayRef[Str]], levels_upto => Level, coderef => Optional[CodeRef], ] $conf

 my $l = Log::Contextual::SimpleLogger->new({
   levels  => [qw( info warn )],
   coderef => sub { print @_ }, # the default prints to STDERR
 });

or

 my $l = Log::Contextual::SimpleLogger->new({
   levels_upto => 'debug',
   coderef     => sub { print @_ }, # the default prints to STDERR
 });

Creates a new SimpleLogger object with the passed levels enabled and optionally a CodeRef may be passed to modify how the logs are output/stored.

levels_upto enables all the levels upto and including the level passed.

Levels may contain:

 trace
 debug
 info
 warn
 error
 fatal

$level

Arguments: @anything

All of the following six methods work the same. The basic pattern is:

 sub $level {
   my $self = shift;

   print STDERR "[$level] " . join qq{\n}, @_;
      if $self->is_$level;
 }

trace

 $l->trace( 'entered method foo with args ' join q{,}, @args );

debug

 $l->debug( 'entered method foo' );

info

 $l->info( 'started process foo' );

warn

 $l->warn( 'possible misconfiguration at line 10' );

error

 $l->error( 'non-numeric user input!' );

fatal

 $l->fatal( '1 is never equal to 0!' );

is_$level

All of the following six functions just return true if their respective level is enabled.

is_trace

 say 'tracing' if $l->is_trace;

is_debug

 say 'debuging' if $l->is_debug;

is_info

 say q{info'ing} if $l->is_info;

is_warn

 say 'warning' if $l->is_warn;

is_error

 say 'erroring' if $l->is_error;

is_fatal

 say q{fatal'ing} if $l->is_fatal;

AUTHOR

Top

See "AUTHOR" in Log::Contextual

COPYRIGHT

Top

LICENSE

Top

See "LICENSE" in Log::Contextual


Log-Contextual documentation Contained in the Log-Contextual distribution.

package Log::Contextual::SimpleLogger;

use strict;
use warnings;

{
  for my $name (qw( trace debug info warn error fatal )) {

    no strict 'refs';

    *{$name} = sub {
      my $self = shift;

      $self->_log( $name, @_ )
        if ($self->{$name});
    };

    *{"is_$name"} = sub {
      my $self = shift;
      return $self->{$name};
    };
  }
}

sub new {
  my ($class, $args) = @_;
  my $self = bless {}, $class;

  $self->{$_} = 1 for @{$args->{levels}};
  $self->{coderef} = $args->{coderef} || sub { print STDERR @_};

  if (my $upto = $args->{levels_upto}) {

    my @levels = (qw( trace debug info warn error fatal ));
    my $i = 0;
    for (@levels) {
      last if $upto eq $_;
      $i++
    }
    for ($i..$#levels) {
      $self->{$levels[$_]} = 1
    }

  }
  return $self;
}

sub _log {
  my $self    = shift;
  my $level   = shift;
  my $message = join( "\n", @_ );
  $message .= "\n" unless $message =~ /\n$/;
  $self->{coderef}->(sprintf( "[%s] %s", $level, $message ));
}

1;

__END__