1 package Log::Contextual::SimpleLogger;
7 for my $name (qw( trace debug info warn error fatal )) {
14 $self->_log( $name, @_ )
20 return $self->{$name};
26 my ($class, $args) = @_;
27 my $self = bless {}, $class;
29 $self->{$_} = 1 for @{$args->{levels}};
30 $self->{coderef} = $args->{coderef} || sub { print STDERR @_};
37 my $message = join( "\n", @_ );
38 $message .= "\n" unless $message =~ /\n$/;
39 $self->{coderef}->(sprintf( "[%s] %s", $level, $message ));
48 Log::Contextual::SimpleLogger - Super simple logger made for playing with Log::Contextual
52 use Log::Contextual qw( :log ),
53 -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
55 log_info { 'program started' }; # no-op because info is not in levels
57 log_debug { 'entered foo' };
63 This module is a simple logger made mostly for demonstration and initial
64 experimentation with L<Log::Contextual>. We recommend you use a real logger
65 instead. For something more serious but not overly complicated, take a look at
72 Arguments: Dict[ levels => ArrayRef[Str], coderef => Optional[CodeRef] ] $conf
74 my $l = Log::Contextual::SimpleLogger->new({
75 levels => [qw( info warn )],
76 coderef => sub { print @_ }, # the default prints to STDERR
79 Creates a new SimpleLogger object with the passed levels enabled and optionally
80 a C<CodeRef> may be passed to modify how the logs are output/stored.
95 All of the following six methods work the same. The basic pattern is:
100 print STDERR "[$level] " . join qq{\n}, @_;
106 $l->trace( 'entered method foo with args ' join q{,}, @args );
110 $l->debug( 'entered method foo' );
114 $l->info( 'started process foo' );
118 $l->warn( 'possible misconfiguration at line 10' );
122 $l->error( 'non-numeric user input!' );
126 $l->fatal( '1 is never equal to 0!' );
130 All of the following six functions just return true if their respective
135 say 'tracing' if $l->is_trace;
139 say 'debuging' if $l->is_debug;
143 say q{info'ing} if $l->is_info;
147 say 'warning' if $l->is_warn;
151 say 'erroring' if $l->is_error;
155 say q{fatal'ing} if $l->is_fatal;
159 See L<Log::Contextual/"AUTHOR">
163 See L<Log::Contextual/"COPYRIGHT">
167 See L<Log::Contextual/"LICENSE">