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