1 package Log::Contextual::WarnLogger;
7 my @levels = (qw( trace debug info warn error fatal ));
8 my %level_num; @level_num{ @levels } = (0 .. $#levels);
9 for my $name (@levels) {
13 my $is_name = "is_$name";
17 $self->_log( $name, @_ )
23 return 1 if $ENV{$self->{env_prefix} . '_' . uc $name};
24 my $upto = $ENV{$self->{env_prefix} . '_UPTO'};
28 return $level_num{$name} >= $level_num{$upto};
34 my ($class, $args) = @_;
35 my $self = bless {}, $class;
37 $self->{env_prefix} = $args->{env_prefix} or
38 die 'no env_prefix passed to Log::Contextual::WarnLogger->new';
45 my $message = join( "\n", @_ );
46 $message .= "\n" unless $message =~ /\n$/;
47 warn "[$level] $message";
56 Log::Contextual::WarnLogger - Simple logger for libraries using Log::Contextual
61 use Log::Contextual::WarnLogger;
62 use Log::Contextual qw( :log ),
63 -default_logger => Log::Contextual::WarnLogger->new({
64 env_prefix => 'MY_PACKAGE'
67 # warns '[info] program started' if $ENV{MY_PACKAGE_TRACE} is set
68 log_info { 'program started' }; # no-op because info is not in levels
70 # warns '[debug] entered foo' if $ENV{MY_PACKAGE_DEBUG} is set
71 log_debug { 'entered foo' };
77 This module is a simple logger made for libraries using L<Log::Contextual>. We
78 recommend the use of this logger as your default logger as it is simple and
79 useful for most users, yet users can use L<Log::Contextual/set_logger> to override
80 your choice of logger in their own code thanks to the way L<Log::Contextual>
87 Arguments: C<< Dict[ env_prefix => Str ] $conf >>
89 my $l = Log::Contextual::WarnLogger->new({
93 Creates a new logger object where C<env_prefix> defines what the prefix is for
94 the environment variables that will be checked for the six log levels. For
95 example, if C<env_prefix> is set to C<FREWS_PACKAGE> the following environment
96 variables will be used:
107 Note that C<UPTO> is a convenience variable. If you set
108 C<< FOO_UPTO=TRACE >> it will enable all log levels. Similarly, if you
109 set it to C<FATAL> only fatal will be enabled.
113 Arguments: C<@anything>
115 All of the following six methods work the same. The basic pattern is:
120 warn "[$level] " . join qq{\n}, @_;
126 $l->trace( 'entered method foo with args ' join q{,}, @args );
130 $l->debug( 'entered method foo' );
134 $l->info( 'started process foo' );
138 $l->warn( 'possible misconfiguration at line 10' );
142 $l->error( 'non-numeric user input!' );
146 $l->fatal( '1 is never equal to 0!' );
150 All of the following six functions just return true if their respective
151 environment variable is enabled.
155 say 'tracing' if $l->is_trace;
159 say 'debuging' if $l->is_debug;
163 say q{info'ing} if $l->is_info;
167 say 'warning' if $l->is_warn;
171 say 'erroring' if $l->is_error;
175 say q{fatal'ing} if $l->is_fatal;
179 See L<Log::Contextual/"AUTHOR">
183 See L<Log::Contextual/"COPYRIGHT">
187 See L<Log::Contextual/"LICENSE">