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?

4

2 回答 2

3

[更新]

我重新阅读了你的问题,我认为我们可以更接近。

将以下内容添加到您的 logback.xml:

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

如Logback Setup page中所述,将 Codehaus Janino 和 Apache commons-compiler jar 添加到您的类路径中。

现在你启动你的应用程序

-Dcom.slf4j.trace=com.example.package.MyClass
于 2012-11-15T01:38:30.310 回答
0

来自 logback 文档的一些想法:

Logback-classic 可以扫描其配置文件中的更改,并在配置文件更改时自动重新配置自己。

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

此外,您可以将默认配置文件的位置指定为系统属性:

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

所以它比你描述的更方便。

于 2012-11-15T00:54:51.017 回答