1 package Log::Contextual::SimpleLogger;
3 # ABSTRACT: Super simple logger made for playing with Log::Contextual
10 for my $name (qw( trace debug info warn error fatal )) {
17 $self->_log($name, @_)
23 return $self->{$name};
29 my ($class, $args) = @_;
30 my $self = bless {}, $class;
32 $self->{$_} = 1 for @{$args->{levels}};
33 $self->{coderef} = $args->{coderef} || sub { print STDERR @_ };
35 if (my $upto = $args->{levels_upto}) {
37 my @levels = (qw( trace debug info warn error fatal ));
43 for ($i .. $#levels) {
44 $self->{$levels[$_]} = 1
54 my $message = join("\n", @_);
55 $message .= "\n" unless $message =~ /\n$/;
56 $self->{coderef}->(sprintf("[%s] %s", $level, $message));
65 use Log::Contextual::SimpleLogger;
66 use Log::Contextual qw( :log ),
67 -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
69 log_info { 'program started' }; # no-op because info is not in levels
71 log_debug { 'entered foo' };
77 This module is a simple logger made mostly for demonstration and initial
78 experimentation with L<Log::Contextual>. We recommend you use a real logger
79 instead. For something more serious but not overly complicated, take a look at
87 levels => Optional[ArrayRef[Str]],
89 coderef => Optional[CodeRef],
92 my $l = Log::Contextual::SimpleLogger->new({
93 levels => [qw( info warn )],
94 coderef => sub { print @_ }, # the default prints to STDERR
99 my $l = Log::Contextual::SimpleLogger->new({
100 levels_upto => 'debug',
101 coderef => sub { print @_ }, # the default prints to STDERR
104 Creates a new SimpleLogger object with the passed levels enabled and optionally
105 a C<CodeRef> may be passed to modify how the logs are output/stored.
107 C<levels_upto> enables all the levels upto and including the level passed.
120 Arguments: C<@anything>
122 All of the following six methods work the same. The basic pattern is:
127 print STDERR "[$level] " . join qq{\n}, @_;
133 $l->trace( 'entered method foo with args ' join q{,}, @args );
137 $l->debug( 'entered method foo' );
141 $l->info( 'started process foo' );
145 $l->warn( 'possible misconfiguration at line 10' );
149 $l->error( 'non-numeric user input!' );
153 $l->fatal( '1 is never equal to 0!' );
157 All of the following six functions just return true if their respective
162 say 'tracing' if $l->is_trace;
166 say 'debuging' if $l->is_debug;
170 say q{info'ing} if $l->is_info;
174 say 'warning' if $l->is_warn;
178 say 'erroring' if $l->is_error;
182 say q{fatal'ing} if $l->is_fatal;