1 package Log::Contextual::WarnLogger;
8 my @default_levels = qw( trace debug info warn error fatal );
11 # generate subs to handle the default levels
12 # anything else will have to be handled by AUTOLOAD at runtime
14 for my $level (@default_levels) {
18 my $is_name = "is_$level";
22 $self->_log( $level, @_ )
28 return 1 if $ENV{$self->{env_prefix} . '_' . uc $level};
29 my $upto = $ENV{$self->{env_prefix} . '_UPTO'};
33 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 ];
94 my %level_num; @level_num{ @$levels } = (0 .. $#{$levels});
98 level_num => \%level_num,
99 custom_levels => $custom_levels,
102 $self->{env_prefix} = $args->{env_prefix} or
103 die 'no env_prefix passed to Log::Contextual::WarnLogger->new';
110 my $message = join( "\n", @_ );
111 $message .= "\n" unless $message =~ /\n$/;
112 warn "[$level] $message";
121 Log::Contextual::WarnLogger - Simple logger for libraries using Log::Contextual
126 use Log::Contextual::WarnLogger;
127 use Log::Contextual qw( :log ),
128 -default_logger => Log::Contextual::WarnLogger->new({
129 env_prefix => 'MY_PACKAGE',
130 levels => [ qw(debug info notice warning error critical alert emergency) ],
133 # warns '[info] program started' if $ENV{MY_PACKAGE_TRACE} is set
134 log_info { 'program started' }; # no-op because info is not in levels
136 # warns '[debug] entered foo' if $ENV{MY_PACKAGE_DEBUG} is set
137 log_debug { 'entered foo' };
145 This module is a simple logger made for libraries using L<Log::Contextual>. We
146 recommend the use of this logger as your default logger as it is simple and
147 useful for most users, yet users can use L<Log::Contextual/set_logger> to override
148 your choice of logger in their own code thanks to the way L<Log::Contextual>
155 Arguments: C<< Dict[ env_prefix => Str, levels => List ] $conf >>
157 my $l = Log::Contextual::WarnLogger->new({
163 my $l = Log::Contextual::WarnLogger->new({
165 levels => [ 'level1', 'level2' ]
169 Creates a new logger object where C<env_prefix> defines what the prefix is for
170 the environment variables that will be checked for the log levels.
172 The log levels may be customized, but if not defined, these are used:
190 For example, if C<env_prefix> is set to C<FREWS_PACKAGE> the following environment
191 variables will be used:
202 Note that C<UPTO> is a convenience variable. If you set
203 C<< FOO_UPTO=TRACE >> it will enable all log levels. Similarly, if you
204 set it to C<FATAL> only fatal will be enabled.
210 Arguments: C<@anything>
212 All of the following six methods work the same. The basic pattern is:
217 warn "[$level] " . join qq{\n}, @_;
223 $l->trace( 'entered method foo with args ' join q{,}, @args );
227 $l->debug( 'entered method foo' );
231 $l->info( 'started process foo' );
235 $l->warn( 'possible misconfiguration at line 10' );
239 $l->error( 'non-numeric user input!' );
243 $l->fatal( '1 is never equal to 0!' );
245 If different levels are specified, appropriate functions named for your custom
246 levels work as you expect.
250 All of the following six functions just return true if their respective
251 environment variable is enabled.
255 say 'tracing' if $l->is_trace;
259 say 'debuging' if $l->is_debug;
263 say q{info'ing} if $l->is_info;
267 say 'warning' if $l->is_warn;
271 say 'erroring' if $l->is_error;
275 say q{fatal'ing} if $l->is_fatal;
277 If different levels are specified, appropriate is_$level functions work as you
282 See L<Log::Contextual/"AUTHOR">
286 See L<Log::Contextual/"COPYRIGHT">
290 See L<Log::Contextual/"LICENSE">