How to set Log4perl log level on the command line?

736 Views Asked by At

Is it possible to set the log level globally for Log4perl by passing a command line flag or setting an environment variable (and not changing the original logging setup)? The documentation and FAQ don't seem to mention any way to do this.

2

There are 2 best solutions below

0
Denis Ibaev On

You may use somthing like this:

Log::Log4perl->get_logger()->level(
    Log::Log4perl::Level::to_priority($ENV{APP_LOG_LEVEL})
);

and run script:

APP_LOG_LEVEL=DEBUG perl app.pl
1
user1934428 On

If you use the configuration file syntax to define your appenders (note that you don't have to place this into a real file; you can also have the configuration definition inside your program), you already have environment variable substitution out of the box. See the section named "Variable Substitution" in the Log4Perl documentation.