Is it possible to configure logback logger levels on the command line?

bfancher picture bfancher · Apr 23, 2014 · Viewed 28.8k times · Source

Log4J allows you to pass the level you'd like particular loggers in your application to log at from the command line, e.g. "-Dlog4j.logger.com.whatever.MyClass=DEBUG". I can't find any similar facility in Logback. According to the FAQ, all it seems to allow you to do is set the level for the root logger by using variable substitution. Is there something I'm missing or does Logback just not support this? Thanks.

Answer

kan picture kan · Apr 23, 2014

Yes, seems there is no such feature. As I understand, it is partly because that logback configuration is more complex so quite difficult to achieve reasonable configuration flexibility by flat string properties. Partly because, imho, it encourages bad practice - placing too many system properties - leads to bloated run-scripts or command lines, more difficult to manage than separate logging configuration files.

However, I could suggest several options:

  • use ${sys.prop.var.name} substitutions in your logback.xml config file
  • copy logback.xml locally, modify it with desired logger levels and specify -Dlogback.configurationFile=/path/to/customised/logback.xml. Keep in mind that "Logback-classic can scan for changes in its configuration file and automatically reconfigure itself when the configuration file changes". So, you even don't need to relaunch your process to change logging levels.
  • DIY: Add a code in your application which would read system properties and apply the properties to the logging levels during start up.