3

Is there a way to change the logging level of a class using a System Property in the launcher? I want to be able to easily and quickly change the debug level of a class depending on which class I'm working on. Being able to do it at launch time with something like -Dcom.example.package.myclass.logger.level=TRACE
or
-Dcom.slf4j.trace=com.example.package.myclass

would be very useful.

I'm using slf4j/logback but i'm also interested in a way to do it in log4j

I know how to change the level programatically, but i don't want to have to change code, just the launcher

If it's relevant, I'm using a configuration file logback-test.xml

If it's not possible, is there another trick to do this without changing code or having to pollute the clean xml file?

2 Answers 2

4

[update]

I re-read your question and I think we can get closer.

Add the following to your logback.xml:

 <if condition='isDefined("com.slf4j.trace")'> <then> <logger name="${com.slf4j.trace}" level="TRACE"/> </then> </if> 

Add the Codehaus Janino and Apache commons-compiler jars to your classpath as described on the Logback Setup page.

Now you launch your app with

-Dcom.slf4j.trace=com.example.package.MyClass 
Sign up to request clarification or add additional context in comments.

2 Comments

this works nicely, thanks! the one problem is the conditional config requires two extra dependencies
True. It's always something :)
0

Some ideas from logback docs:

Logback-classic can scan for changes in its configuration file and automatically reconfigure itself when the configuration file changes.

<configuration scan="true" scanPeriod="30 seconds" > ... </configuration> 

Also, you can specify the location of the default configuration file as a system property:

java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1 

So it's even more convenient than you described it.

4 Comments

The goal of reconfiguration scan is different: it's so that you can change the level of a running application. That's not what i want, i want to change the level before starting it but without touching the configuration file since if i modify it while testing, the changes will make eclipse want to commit the file
Still you can use java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1 and mark this config file as ignored in you source code repository. This is similar to what you want.
ok, i see your point: Creating a working log configuration that i don't commit. It's not ideal but it might be my only chance
You can also think about implementing that by your own. In some static init block you can scan for such a properties and set logging level pragmatically.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.