Skip to content

Commit

Permalink
Respect LOG_CATEGORY_ALIAS, LOG_CATEGORY_LEVEL and ${OUT}_CATEGORY_LEVEL
Browse files Browse the repository at this point in the history
  • Loading branch information
sharyanto committed Feb 25, 2012
1 parent f99e3ec commit 0f7ae9f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions dist.ini
Expand Up @@ -36,6 +36,7 @@ Log::Dispatch::FileRotate = 0
Log::Dispatch::Syslog = 0 Log::Dispatch::Syslog = 0
File::Path = 2.07 File::Path = 2.07
Log::Dispatch::Dir = 0.08 Log::Dispatch::Dir = 0.08
JSON=0


; testing ; testing
File::Slurp = 0 File::Slurp = 0
Expand Down
47 changes: 42 additions & 5 deletions lib/Log/Any/App.pm
Expand Up @@ -42,6 +42,17 @@ sub _ifdef {
defined($a) ? $a : $b; defined($a) ? $a : $b;
} }


# m=multi, j=as json (except the last default)
sub _ifdefmj {
require JSON;

my $def = pop @_;
for (@_) {
return JSON::decode_json($_) if defined($_);
}
$def;
}

sub init { sub init {
return if $init_called++; return if $init_called++;


Expand Down Expand Up @@ -310,7 +321,7 @@ sub _parse_opts {
init => 1, init => 1,
dump => ($ENV{LOGANYAPP_DEBUG} ? 1:0), dump => ($ENV{LOGANYAPP_DEBUG} ? 1:0),
daemon => 0, daemon => 0,
category_alias => {}, category_alias => _ifdefmj($ENV{LOG_CATEGORY_ALIAS}, {}),
level_flag_paths => [File::HomeDir->my_home, "/etc"], level_flag_paths => [File::HomeDir->my_home, "/etc"],
}; };


Expand Down Expand Up @@ -483,7 +494,9 @@ sub _default_file {
} }
return { return {
level => $level, level => $level,
category_level => $spec->{category_level}, category_level => _ifdefmj($ENV{FILE_LOG_CATEGORY_LEVEL},
$ENV{LOG_CATEGORY_LEVEL},
$spec->{category_level}),
path => $> ? File::Spec->catfile(File::HomeDir->my_home, "$spec->{name}.log") : path => $> ? File::Spec->catfile(File::HomeDir->my_home, "$spec->{name}.log") :
"/var/log/$spec->{name}.log", # XXX and on Windows? "/var/log/$spec->{name}.log", # XXX and on Windows?
max_size => undef, max_size => undef,
Expand Down Expand Up @@ -530,7 +543,9 @@ sub _default_dir {
} }
return { return {
level => $level, level => $level,
category_level => $spec->{category_level}, category_level => _ifdefmj($ENV{DIR_LOG_CATEGORY_LEVEL},
$ENV{LOG_CATEGORY_LEVEL},
$spec->{category_level}),
path => $> ? File::Spec->catfile(File::HomeDir->my_home, "log", $spec->{name}) : path => $> ? File::Spec->catfile(File::HomeDir->my_home, "log", $spec->{name}) :
"/var/log/$spec->{name}", # XXX and on Windows? "/var/log/$spec->{name}", # XXX and on Windows?
max_size => undef, max_size => undef,
Expand Down Expand Up @@ -567,7 +582,9 @@ sub _default_screen {
color => _ifdef($ENV{COLOR}, (-t STDOUT)), color => _ifdef($ENV{COLOR}, (-t STDOUT)),
stderr => 1, stderr => 1,
level => $level, level => $level,
category_level => $spec->{category_level}, category_level => _ifdefmj($ENV{SCREEN_LOG_CATEGORY_LEVEL},
$ENV{LOG_CATEGORY_LEVEL},
$spec->{category_level}),
category => '', category => '',
pattern_style => ($ENV{LOG_SHOW_CATEGORY} ? 'cat_':'') . 'script_short', pattern_style => ($ENV{LOG_SHOW_CATEGORY} ? 'cat_':'') . 'script_short',
pattern => undef, pattern => undef,
Expand All @@ -591,7 +608,9 @@ sub _default_syslog {
} }
return { return {
level => $level, level => $level,
category_level => $spec->{category_level}, category_level => _ifdefmj($ENV{SYSLOG_LOG_CATEGORY_LEVEL},
$ENV{LOG_CATEGORY_LEVEL},
$spec->{category_level}),
ident => $spec->{name}, ident => $spec->{name},
facility => 'daemon', facility => 'daemon',
pattern_style => ($ENV{LOG_SHOW_CATEGORY} ? 'cat_':'') . 'syslog', pattern_style => ($ENV{LOG_SHOW_CATEGORY} ? 'cat_':'') . 'syslog',
Expand Down Expand Up @@ -1264,12 +1283,22 @@ you can do this instead:
-screen => [category=>'-fbb', ...], -screen => [category=>'-fbb', ...],
); );
You can also specify this from the environment variable LOG_CATEGORY_ALIAS using
JSON encoding, e.g.
LOG_CATEGORY_ALIAS='{"-fbb":["Foo","Bar","Baz"]}'
=item -category_level => {CATEGORY=>LEVEL, ...} =item -category_level => {CATEGORY=>LEVEL, ...}
Specify per-category level. Categories not mentioned on this will use the Specify per-category level. Categories not mentioned on this will use the
general level (-level). This can be used to increase or decrease logging on general level (-level). This can be used to increase or decrease logging on
certain categories/modules. certain categories/modules.
You can also specify this from the environment variable LOG_CATEGORY_LEVEL using
JSON encoding, e.g.
LOG_CATEGORY_LEVEL='{"-fbb":"off"}'
=item -level => 'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'off' =item -level => 'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'off'
Specify log level for all outputs. Each output can override this value. The Specify log level for all outputs. Each output can override this value. The
Expand Down Expand Up @@ -1338,6 +1367,8 @@ App::options, command line, environment, level flag file, and package variables
in main are also searched first (for B<FILE_LOG_LEVEL>, B<FILE_TRACE>, in main are also searched first (for B<FILE_LOG_LEVEL>, B<FILE_TRACE>,
B<FILE_DEBUG>, B<FILE_VERBOSE>, B<FILE_QUIET>, and the similars). B<FILE_DEBUG>, B<FILE_VERBOSE>, B<FILE_QUIET>, and the similars).
You can also specify category level from environment FILE_LOG_CATEGORY_LEVEL.
=item -dir => 0 | 1|yes|true | PATH | {opts} | [{opts}, ...] =item -dir => 0 | 1|yes|true | PATH | {opts} | [{opts}, ...]
Log messages using L<Log::Dispatch::Dir>. Each message is logged into separate Log messages using L<Log::Dispatch::Dir>. Each message is logged into separate
Expand Down Expand Up @@ -1375,6 +1406,8 @@ But App::options, command line, environment, level flag file, and package
variables in main are also searched first (for B<DIR_LOG_LEVEL>, B<DIR_TRACE>, variables in main are also searched first (for B<DIR_LOG_LEVEL>, B<DIR_TRACE>,
B<DIR_DEBUG>, B<DIR_VERBOSE>, B<DIR_QUIET>, and the similars). B<DIR_DEBUG>, B<DIR_VERBOSE>, B<DIR_QUIET>, and the similars).
You can also specify category level from environment DIR_LOG_CATEGORY_LEVEL.
=item -screen => 0 | 1|yes|true | {opts} =item -screen => 0 | 1|yes|true | {opts}
Log messages using L<Log::Log4perl::Appender::ScreenColoredLevels>. Log messages using L<Log::Log4perl::Appender::ScreenColoredLevels>.
Expand All @@ -1401,6 +1434,8 @@ similars).
Color can also be turned on/off using environment variable COLOR (if B<color> Color can also be turned on/off using environment variable COLOR (if B<color>
argument is not set). argument is not set).
You can also specify category level from environment SCREEN_LOG_CATEGORY_LEVEL.
=item -syslog => 0 | 1|yes|true | {opts} =item -syslog => 0 | 1|yes|true | {opts}
Log messages using L<Log::Dispatch::Syslog>. Log messages using L<Log::Dispatch::Syslog>.
Expand All @@ -1427,6 +1462,8 @@ package variables in main are also searched first (for B<SYSLOG_LOG_LEVEL>,
B<SYSLOG_TRACE>, B<SYSLOG_DEBUG>, B<SYSLOG_VERBOSE>, B<SYSLOG_QUIET>, and the B<SYSLOG_TRACE>, B<SYSLOG_DEBUG>, B<SYSLOG_VERBOSE>, B<SYSLOG_QUIET>, and the
similars). similars).
You can also specify category level from environment SYSLOG_LOG_CATEGORY_LEVEL.
=item -dump => BOOL =item -dump => BOOL
If set to true then Log::Any::App will dump the generated Log4perl config. If set to true then Log::Any::App will dump the generated Log4perl config.
Expand Down
3 changes: 3 additions & 0 deletions t/01-basics.t
Expand Up @@ -229,4 +229,7 @@ test_init(
# XXX setting general level via app::options # XXX setting general level via app::options
# XXX setting output level via app::options # XXX setting output level via app::options


# XXX test LOG_SHOW_CATEGORY
# XXX test LOG_CATEGORY_ALIAS & LOG_CATEGORY_LEVEL & ${OUT}_LOG_CATEGORY_LEVEL

done_testing(); done_testing();

0 comments on commit 0f7ae9f

Please sign in to comment.