1
votes

I use slf4j 1.7.2 with logback 1.0.7. I want to be able to supply a command line parameter to my Java application that sets the level of messages logged to stdout. For example, -logLevel trace should result in all messages logged but -logLevel warning only in warnings and errors. The logs should come from all loggers in the application (= the root logger).

Currently I have a ConsoleAppender for System.out that ensures, by means of TresholdFilter, that only info warning and error messages are printed out to stdout. The root logger references it. I don't know how to programatically change the filtered levels for this appender, which I need to base the logged levels on a command line parameter.

While the levels logged to stdout should be influenced, I still want for the root logger to log all levels, as they are sent to other appenders, e.g. diagnostics.txt file that should have all the levels all the time.

Cheers!
Konrad

1

1 Answers

2
votes

Logback's configuration file allows the use of variables. You can define a System property (-D parameter to the JVM), and the use that value in the configuration file. The syntax is:

${VARIABLE_NAME}

You should be able to define a variable, say -DSYSOUT_LOG_LEVEL=ERROR Then, in your logback.xml file, instead of setting a specific level for the filter, you can use ${SYSOUT_LOG_LEVEL}, e.g.

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>${SYSOUT_LOG_LEVEL}</level>
</filter>