Hilikus
Hilikus

Reputation: 10331

Change logger level for single class through system property

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?

Upvotes: 3

Views: 2007

Answers (2)

Guido Simone
Guido Simone

Reputation: 7952

[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

Upvotes: 4

Maciej Ziarko
Maciej Ziarko

Reputation: 12114

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.

Upvotes: 0

Related Questions