qw() > qw{}
[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 qw( :log ),
53    -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )]});
54
55  log_info { 'program started' }; # no-op because info is not in levels
56  sub foo {
57    log_debug { 'entered foo' };
58    ...
59  }
60
61 =head1 DESCRIPTION
62
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
66 L<Log::Dispatchouli>.
67
68 =head1 METHODS
69
70 =head2 new
71
72 Arguments: Dict[ levels => ArrayRef[Str], coderef => Optional[CodeRef] ] $conf
73
74  my $l = Log::Contextual::SimpleLogger->new({
75    levels => [qw( info warn )],
76    coderef => sub { print @_ }, # the default prints to STDERR
77  });
78
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.
81
82 Levels may contain:
83
84  trace
85  debug
86  info
87  warn
88  error
89  fatal
90
91 =head2 $level
92
93 Arguments: @anything
94
95 All of the following six methods work the same.  The basic pattern is:
96
97  sub $level {
98    my $self = shift;
99
100    print STDERR "[$level] " . join qq{\n}, @_;
101       if $self->is_$level;
102  }
103
104 =head3 trace
105
106  $l->trace( 'entered method foo with args ' join q{,}, @args );
107
108 =head3 debug
109
110  $l->debug( 'entered method foo' );
111
112 =head3 info
113
114  $l->info( 'started process foo' );
115
116 =head3 warn
117
118  $l->warn( 'possible misconfiguration at line 10' );
119
120 =head3 error
121
122  $l->error( 'non-numeric user input!' );
123
124 =head3 fatal
125
126  $l->fatal( '1 is never equal to 0!' );
127
128 =head2 is_$level
129
130 All of the following six functions just return true if their respective
131 level is enabled.
132
133 =head3 is_trace
134
135  say 'tracing' if $l->is_trace;
136
137 =head3 is_debug
138
139  say 'debuging' if $l->is_debug;
140
141 =head3 is_info
142
143  say q{info'ing} if $l->is_info;
144
145 =head3 is_warn
146
147  say 'warning' if $l->is_warn;
148
149 =head3 is_error
150
151  say 'erroring' if $l->is_error;
152
153 =head3 is_fatal
154
155  say q{fatal'ing} if $l->is_fatal;
156
157 =head1 AUTHOR
158
159 See L<Log::Contextual/"AUTHOR">
160
161 =head1 COPYRIGHT
162
163 See L<Log::Contextual/"COPYRIGHT">
164
165 =head1 LICENSE
166
167 See L<Log::Contextual/"LICENSE">
168
169 =cut
170