perltidy code and add .perltidyrc to repo
[p5sagit/Log-Contextual.git] / lib / Log / Contextual / SimpleLogger.pm
CommitLineData
88c83a83 1package Log::Contextual::SimpleLogger;
2
3use strict;
4use warnings;
5
6{
6ae293d7 7 for my $name (qw( trace debug info warn error fatal )) {
88c83a83 8
6ae293d7 9 no strict 'refs';
88c83a83 10
6ae293d7 11 *{$name} = sub {
12 my $self = shift;
88c83a83 13
6ae293d7 14 $self->_log($name, @_)
15 if ($self->{$name});
16 };
88c83a83 17
6ae293d7 18 *{"is_$name"} = sub {
19 my $self = shift;
20 return $self->{$name};
21 };
22 }
88c83a83 23}
24
25sub new {
6ae293d7 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
32 if (my $upto = $args->{levels_upto}) {
33
34 my @levels = (qw( trace debug info warn error fatal ));
35 my $i = 0;
36 for (@levels) {
37 last if $upto eq $_;
38 $i++
39 }
40 for ($i .. $#levels) {
41 $self->{$levels[$_]} = 1
42 }
43
44 }
45 return $self;
88c83a83 46}
47
48sub _log {
6ae293d7 49 my $self = shift;
50 my $level = shift;
51 my $message = join("\n", @_);
52 $message .= "\n" unless $message =~ /\n$/;
53 $self->{coderef}->(sprintf("[%s] %s", $level, $message));
88c83a83 54}
55
561;
57
8bc568d2 58__END__
59
60=head1 NAME
61
62Log::Contextual::SimpleLogger - Super simple logger made for playing with Log::Contextual
63
64=head1 SYNOPSIS
65
5dfb93e6 66 use Log::Contextual::SimpleLogger;
9b8e24d5 67 use Log::Contextual qw( :log ),
68 -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
8bc568d2 69
70 log_info { 'program started' }; # no-op because info is not in levels
71 sub foo {
72 log_debug { 'entered foo' };
73 ...
74 }
75
76=head1 DESCRIPTION
77
78This module is a simple logger made mostly for demonstration and initial
79experimentation with L<Log::Contextual>. We recommend you use a real logger
80instead. For something more serious but not overly complicated, take a look at
81L<Log::Dispatchouli>.
82
83=head1 METHODS
84
85=head2 new
86
13955c53 87Arguments: C<< Dict[
88 levels => Optional[ArrayRef[Str]],
89 levels_upto => Level,
90 coderef => Optional[CodeRef],
91] $conf >>
8bc568d2 92
93 my $l = Log::Contextual::SimpleLogger->new({
13955c53 94 levels => [qw( info warn )],
8bc568d2 95 coderef => sub { print @_ }, # the default prints to STDERR
96 });
97
13955c53 98or
99
100 my $l = Log::Contextual::SimpleLogger->new({
101 levels_upto => 'debug',
102 coderef => sub { print @_ }, # the default prints to STDERR
103 });
104
8bc568d2 105Creates a new SimpleLogger object with the passed levels enabled and optionally
106a C<CodeRef> may be passed to modify how the logs are output/stored.
107
13955c53 108C<levels_upto> enables all the levels upto and including the level passed.
109
8bc568d2 110Levels may contain:
111
112 trace
113 debug
114 info
115 warn
116 error
117 fatal
118
119=head2 $level
120
5dfb93e6 121Arguments: C<@anything>
8bc568d2 122
123All of the following six methods work the same. The basic pattern is:
124
125 sub $level {
126 my $self = shift;
127
128 print STDERR "[$level] " . join qq{\n}, @_;
129 if $self->is_$level;
130 }
131
132=head3 trace
133
134 $l->trace( 'entered method foo with args ' join q{,}, @args );
135
136=head3 debug
137
138 $l->debug( 'entered method foo' );
139
140=head3 info
141
142 $l->info( 'started process foo' );
143
144=head3 warn
145
146 $l->warn( 'possible misconfiguration at line 10' );
147
148=head3 error
149
150 $l->error( 'non-numeric user input!' );
151
152=head3 fatal
153
154 $l->fatal( '1 is never equal to 0!' );
155
156=head2 is_$level
157
158All of the following six functions just return true if their respective
159level is enabled.
160
161=head3 is_trace
162
163 say 'tracing' if $l->is_trace;
164
165=head3 is_debug
166
167 say 'debuging' if $l->is_debug;
168
169=head3 is_info
170
171 say q{info'ing} if $l->is_info;
172
173=head3 is_warn
174
175 say 'warning' if $l->is_warn;
176
177=head3 is_error
178
179 say 'erroring' if $l->is_error;
180
181=head3 is_fatal
182
183 say q{fatal'ing} if $l->is_fatal;
184
185=head1 AUTHOR
186
187See L<Log::Contextual/"AUTHOR">
188
189=head1 COPYRIGHT
190
191See L<Log::Contextual/"COPYRIGHT">
192
193=head1 LICENSE
194
195See L<Log::Contextual/"LICENSE">
196
197=cut
198