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::SimpleLogger;
53 use Log::Contextual qw( :log ),
54 -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
56 log_info { 'program started' }; # no-op because info is not in levels
58 log_debug { 'entered foo' };
64 This module is a simple logger made mostly for demonstration and initial
65 experimentation with L<Log::Contextual>. We recommend you use a real logger
66 instead. For something more serious but not overly complicated, take a look at
73 Arguments: C<< Dict[ levels => ArrayRef[Str], coderef => Optional[CodeRef] ] $conf >>
75 my $l = Log::Contextual::SimpleLogger->new({
76 levels => [qw( info warn )],
77 coderef => sub { print @_ }, # the default prints to STDERR
80 Creates a new SimpleLogger object with the passed levels enabled and optionally
81 a C<CodeRef> may be passed to modify how the logs are output/stored.
94 Arguments: C<@anything>
96 All of the following six methods work the same. The basic pattern is:
101 print STDERR "[$level] " . join qq{\n}, @_;
107 $l->trace( 'entered method foo with args ' join q{,}, @args );
111 $l->debug( 'entered method foo' );
115 $l->info( 'started process foo' );
119 $l->warn( 'possible misconfiguration at line 10' );
123 $l->error( 'non-numeric user input!' );
127 $l->fatal( '1 is never equal to 0!' );
131 All of the following six functions just return true if their respective
136 say 'tracing' if $l->is_trace;
140 say 'debuging' if $l->is_debug;
144 say q{info'ing} if $l->is_info;
148 say 'warning' if $l->is_warn;
152 say 'erroring' if $l->is_error;
156 say q{fatal'ing} if $l->is_fatal;
160 See L<Log::Contextual/"AUTHOR">
164 See L<Log::Contextual/"COPYRIGHT">
168 See L<Log::Contextual/"LICENSE">