差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
java:log4j2:changeloglevelatruntime [2022/02/04 16:23] tony |
java:log4j2:changeloglevelatruntime [2022/02/04 22:13] tony |
||
---|---|---|---|
行 44: | 行 44: | ||
appender.console.filter.threshold.level = trace | appender.console.filter.threshold.level = trace | ||
- | logger.rolling.name = org.tonylin.practice.log4j2.example1 | + | logger.console.name = org.tonylin.practice.log4j2.example1 |
- | logger.rolling.level = error | + | logger.console.level = error |
- | logger.rolling.additivity = false | + | logger.console.additivity = false |
- | logger.rolling.appenderRef.rolling.ref = STDOUT | + | logger.console.appenderRef.rolling.ref = STDOUT |
</code> | </code> | ||
==== Unit Test ==== | ==== Unit Test ==== | ||
行 68: | 行 68: | ||
<code java> | <code java> | ||
@Test | @Test | ||
- | public void Should_NotConsole_When_LogDebugWithDefaultConfiguration() { | + | public void Should_NotSystemOutToConsole_When_LogDebugWithDefaultConfiguration() { |
TestLogger.debug("test debug"); | TestLogger.debug("test debug"); | ||
assertEquals(0, captor.getStandardOutput().size()); | assertEquals(0, captor.getStandardOutput().size()); | ||
行 74: | 行 74: | ||
@Test | @Test | ||
- | public void Should_Console_When_LogErrorWithDefaultConfiguration() { | + | public void Should_SystemOutToConsole_When_LogErrorWithDefaultConfiguration() { |
TestLogger.error("test error"); | TestLogger.error("test error"); | ||
assertEquals(1, captor.getStandardOutput().size()); | assertEquals(1, captor.getStandardOutput().size()); | ||
行 80: | 行 80: | ||
} | } | ||
</code> | </code> | ||
- | 接著就是本篇主角了。 | + | 接著就是本篇主角了,第一個方法是直接用Configurator去設定,雖然簡單,但這不是一個[[https://logging.apache.org/log4j/2.x/faq.html#reconfig_level_from_code|public的API]],不建議在production code使用: |
<code java> | <code java> | ||
@Test | @Test | ||
- | public void Should_Console_When_LogDebugAfterChangingLogLevel() { | + | public void Should_SystemOutToConsole_When_LogDebugAfterChangingLogLevelWithConfigurator() { |
+ | Configurator.setLevel(TestLogger.class.getName(), Level.DEBUG); | ||
+ | |||
+ | TestLogger.debug("test debug"); | ||
+ | assertEquals(1, captor.getStandardOutput().size()); | ||
+ | assertTrue(captor.getStandardOutput().get(0).contains("test debug")); | ||
+ | } | ||
+ | </code> | ||
+ | 另外一個方法參考自[[https://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2|這裡]],其實就是Configurator實作的方法(Configurator像是一個Utility的Class): | ||
+ | <code java> | ||
+ | @Test | ||
+ | public void Should_SystemOutToConsole_When_LogDebugAfterChangingLogLevel() { | ||
LoggerContext ctx = (LoggerContext) LogManager.getContext(false); | LoggerContext ctx = (LoggerContext) LogManager.getContext(false); | ||
Configuration config = ctx.getConfiguration(); | Configuration config = ctx.getConfiguration(); | ||
行 95: | 行 106: | ||
} | } | ||
</code> | </code> | ||
+ | 註: LogManager.getContext(true)的真正作用情境還不曉得為何,有弄清楚後再分享。 | ||
===== Reference ===== | ===== Reference ===== | ||
* [[https://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2|Programmatically change log level in Log4j2]] | * [[https://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2|Programmatically change log level in Log4j2]] |