1 package Log::Contextual::WarnLogger;
8 my @default_levels = qw( trace debug info warn error fatal );
10 # generate subs to handle the default levels
11 # anything else will have to be handled by AUTOLOAD at runtime
13 for my $level (@default_levels) {
17 my $is_name = "is_$level";
21 $self->_log($level, @_)
27 return 1 if $ENV{$self->{env_prefix} . '_' . uc $level};
28 my $upto = $ENV{$self->{env_prefix} . '_UPTO'};
32 return $self->{level_num}{$level} >= $self->{level_num}{$upto};
42 (my $name = our $AUTOLOAD) =~ s/.*:://;
43 return if $name eq 'DESTROY';
45 # extract the log level from the sub name
46 my ($is, $level) = $name =~ m/^(is_)?(.+)$/;
47 my $is_name = "is_$level";
53 $self->_log($level, @_)
60 my $prefix_field = $self->{env_prefix} . '_' . uc $level;
61 return 1 if $ENV{$prefix_field};
63 # don't log if the variable specifically says not to
64 return 0 if defined $ENV{$prefix_field} and not $ENV{$prefix_field};
66 my $upto_field = $self->{env_prefix} . '_UPTO';
67 my $upto = $ENV{$upto_field};
72 croak "Unrecognized log level '$upto' in \$ENV{$upto_field}"
73 if not defined $self->{level_num}{$upto};
75 return $self->{level_num}{$level} >= $self->{level_num}{$upto};
78 # if we don't recognize this level and nothing says otherwise, log!
79 return 1 if not $self->{custom_levels};
85 my ($class, $args) = @_;
87 my $levels = $args->{levels};
88 croak 'invalid levels specification: must be non-empty arrayref'
89 if defined $levels and (ref $levels ne 'ARRAY' or !@$levels);
91 my $custom_levels = defined $levels;
92 $levels ||= [@default_levels];
95 @level_num{@$levels} = (0 .. $#{$levels});
99 level_num => \%level_num,
100 custom_levels => $custom_levels,
103 $self->{env_prefix} = $args->{env_prefix}
104 or die 'no env_prefix passed to Log::Contextual::WarnLogger->new';
111 my $message = join("\n", @_);
112 $message .= "\n" unless $message =~ /\n$/;
113 warn "[$level] $message";
122 Log::Contextual::WarnLogger - Simple logger for libraries using Log::Contextual
127 use Log::Contextual::WarnLogger;
128 use Log::Contextual qw( :log ),
129 -default_logger => Log::Contextual::WarnLogger->new({
130 env_prefix => 'MY_PACKAGE',
131 levels => [ qw(debug info notice warning error critical alert emergency) ],
134 # warns '[info] program started' if $ENV{MY_PACKAGE_TRACE} is set
135 log_info { 'program started' }; # no-op because info is not in levels
137 # warns '[debug] entered foo' if $ENV{MY_PACKAGE_DEBUG} is set
138 log_debug { 'entered foo' };
146 This module is a simple logger made for libraries using L<Log::Contextual>. We
147 recommend the use of this logger as your default logger as it is simple and
148 useful for most users, yet users can use L<Log::Contextual/set_logger> to override
149 your choice of logger in their own code thanks to the way L<Log::Contextual>
156 Arguments: C<< Dict[ env_prefix => Str, levels => List ] $conf >>
158 my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' });
162 my $l = Log::Contextual::WarnLogger->new({
164 levels => [ 'level1', 'level2' ],
167 Creates a new logger object where C<env_prefix> defines what the prefix is for
168 the environment variables that will be checked for the log levels.
170 The log levels may be customized, but if not defined, these are used:
188 For example, if C<env_prefix> is set to C<FREWS_PACKAGE> the following environment
189 variables will be used:
200 Note that C<UPTO> is a convenience variable. If you set
201 C<< FOO_UPTO=TRACE >> it will enable all log levels. Similarly, if you
202 set it to C<FATAL> only fatal will be enabled.
206 Arguments: C<@anything>
208 All of the following six methods work the same. The basic pattern is:
213 warn "[$level] " . join qq{\n}, @_;
219 $l->trace( 'entered method foo with args ' join q{,}, @args );
223 $l->debug( 'entered method foo' );
227 $l->info( 'started process foo' );
231 $l->warn( 'possible misconfiguration at line 10' );
235 $l->error( 'non-numeric user input!' );
239 $l->fatal( '1 is never equal to 0!' );
241 If different levels are specified, appropriate functions named for your custom
242 levels work as you expect.
246 All of the following six functions just return true if their respective
247 environment variable is enabled.
251 say 'tracing' if $l->is_trace;
255 say 'debuging' if $l->is_debug;
259 say q{info'ing} if $l->is_info;
263 say 'warning' if $l->is_warn;
267 say 'erroring' if $l->is_error;
271 say q{fatal'ing} if $l->is_fatal;
273 If different levels are specified, appropriate is_$level functions work as you
278 See L<Log::Contextual/"AUTHOR">
282 See L<Log::Contextual/"COPYRIGHT">
286 See L<Log::Contextual/"LICENSE">