use warnings;
{
- for my $name (qw( trace debug info warn error fatal )) {
+ my @levels = (qw( trace debug info warn error fatal ));
+ my %level_num; @level_num{ @levels } = (0 .. $#levels);
+ for my $name (@levels) {
no strict 'refs';
*{$is_name} = sub {
my $self = shift;
- return $ENV{$self->{env_prefix} . '_' . uc $name};
+ return 1 if $ENV{$self->{env_prefix} . '_' . uc $name};
+ my $upto = $ENV{$self->{env_prefix} . '_UPTO'};
+ return unless $upto;
+ $upto = lc $upto;
+
+ return $level_num{$name} >= $level_num{$upto};
};
}
}
example, if C<env_prefix> is set to C<FREWS_PACKAGE> the following environment
variables will be used:
+ FREWS_PACKAGE_UPTO
+
FREWS_PACKAGE_TRACE
FREWS_PACKAGE_DEBUG
FREWS_PACKAGE_INFO
FREWS_PACKAGE_ERROR
FREWS_PACKAGE_FATAL
+Note that C<UPTO> is a convenience variable. If you set
+C<< FOO_UPTO=TRACE >> it will enable all log levels. Similarly, if you
+set it to C<FATAL> only fatal will be enabled.
+
=head2 $level
Arguments: C<@anything>
}
{
+ local $ENV{BAR_UPTO} = 'TRACE';
+
+ ok($l->is_trace, 'is_trace is true on WarnLogger');
+ ok($l->is_debug, 'is_debug is true on WarnLogger');
+ ok($l->is_info, 'is_info is true on WarnLogger');
+ ok($l->is_warn, 'is_warn is true on WarnLogger');
+ ok($l->is_error, 'is_error is true on WarnLogger');
+ ok($l->is_fatal, 'is_fatal is true on WarnLogger');
+}
+
+{
+ local $ENV{BAR_UPTO} = 'warn';
+
+ ok(!$l->is_trace, 'is_trace is false on WarnLogger');
+ ok(!$l->is_debug, 'is_debug is false on WarnLogger');
+ ok(!$l->is_info, 'is_info is false on WarnLogger');
+ ok($l->is_warn, 'is_warn is true on WarnLogger');
+ ok($l->is_error, 'is_error is true on WarnLogger');
+ ok($l->is_fatal, 'is_fatal is true on WarnLogger');
+}
+
+{
local $ENV{FOO_TRACE} = 0;
local $ENV{FOO_DEBUG} = 1;
local $ENV{FOO_INFO} = 0;