Use new Log::Log4perl features to clean up caller_depth stuff
[p5sagit/Log-Contextual.git] / lib / Log / Contextual / SimpleLogger.pm
1 package Log::Contextual::SimpleLogger;
2
3 use strict;
4 use warnings;
5
6 {
7   for my $name (qw( trace debug info warn error fatal )) {
8
9     no strict 'refs';
10
11     *{$name} = sub {
12       my $self = shift;
13
14       $self->_log( $name, @_ )
15         if ($self->{$name});
16     };
17
18     *{"is_$name"} = sub {
19       my $self = shift;
20       return $self->{$name};
21     };
22   }
23 }
24
25 sub new {
26   my ($class, $args) = @_;
27   my $self = bless {}, $class;
28
29   $self->{$_} = 1 for @{$args->{levels}};
30   $self->{coderef} = $args->{coderef} || sub { print STDERR @_};
31   return $self;
32 }
33
34 sub _log {
35   my $self    = shift;
36   my $level   = shift;
37   my $message = join( "\n", @_ );
38   $message .= "\n" unless $message =~ /\n$/;
39   $self->{coderef}->(sprintf( "[%s] %s", $level, $message ));
40 }
41
42 1;
43
44 __END__
45
46 =head1 NAME
47
48 Log::Contextual::SimpleLogger - Super simple logger made for playing with Log::Contextual
49
50 =head1 SYNOPSIS
51
52  use Log::Contextual::SimpleLogger;
53  use Log::Contextual qw( :log ),
54    -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
55
56  log_info { 'program started' }; # no-op because info is not in levels
57  sub foo {
58    log_debug { 'entered foo' };
59    ...
60  }
61
62 =head1 DESCRIPTION
63
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
67 L<Log::Dispatchouli>.
68
69 =head1 METHODS
70
71 =head2 new
72
73 Arguments: C<< Dict[ levels => ArrayRef[Str], coderef => Optional[CodeRef] ] $conf >>
74
75  my $l = Log::Contextual::SimpleLogger->new({
76    levels => [qw( info warn )],
77    coderef => sub { print @_ }, # the default prints to STDERR
78  });
79
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.
82
83 Levels may contain:
84
85  trace
86  debug
87  info
88  warn
89  error
90  fatal
91
92 =head2 $level
93
94 Arguments: C<@anything>
95
96 All of the following six methods work the same.  The basic pattern is:
97
98  sub $level {
99    my $self = shift;
100
101    print STDERR "[$level] " . join qq{\n}, @_;
102       if $self->is_$level;
103  }
104
105 =head3 trace
106
107  $l->trace( 'entered method foo with args ' join q{,}, @args );
108
109 =head3 debug
110
111  $l->debug( 'entered method foo' );
112
113 =head3 info
114
115  $l->info( 'started process foo' );
116
117 =head3 warn
118
119  $l->warn( 'possible misconfiguration at line 10' );
120
121 =head3 error
122
123  $l->error( 'non-numeric user input!' );
124
125 =head3 fatal
126
127  $l->fatal( '1 is never equal to 0!' );
128
129 =head2 is_$level
130
131 All of the following six functions just return true if their respective
132 level is enabled.
133
134 =head3 is_trace
135
136  say 'tracing' if $l->is_trace;
137
138 =head3 is_debug
139
140  say 'debuging' if $l->is_debug;
141
142 =head3 is_info
143
144  say q{info'ing} if $l->is_info;
145
146 =head3 is_warn
147
148  say 'warning' if $l->is_warn;
149
150 =head3 is_error
151
152  say 'erroring' if $l->is_error;
153
154 =head3 is_fatal
155
156  say q{fatal'ing} if $l->is_fatal;
157
158 =head1 AUTHOR
159
160 See L<Log::Contextual/"AUTHOR">
161
162 =head1 COPYRIGHT
163
164 See L<Log::Contextual/"COPYRIGHT">
165
166 =head1 LICENSE
167
168 See L<Log::Contextual/"LICENSE">
169
170 =cut
171