我想有一个单一的log4j2属性文件在我的jar中交付,但在启动应用程序时覆盖其设置。例如,默认级别可能是warn,但在调试时,我希望将日志级别更改为debug。我知道log4j.configurationFile系统属性,但这对这个问题来说是一个沉重的打击。我使用的是log4j 2.17.2和Java 11,除了LogManager.getLogger()
之外,我没有做任何特殊的log4j初始化。我的属性文件如下:
status = debug
rootLogger.level = warn
rootLogger.appenderRefs = console
rootLogger.appenderRef.stdout.ref = STDOUT
loggers= dt
logger.dt = console
logger.dt.name = com.ibm.data_triage
logger.dt.appenderRefs = console
logger.dt.appenderRef.stdout.ref = STDOUT
#logger.dt.level = trace
logger.dt.level = info
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-dd HH:mm:ss} %p %l - %m%n
我可以做这样的事情吗?
java -Dlogger.dt.level=trace
或
java -Dlog4j.logger.dt.level=trace
似乎都不起作用。我也试过AbuNassar在How do I set log4j level on the command line?中的解决方案,但没有用。
如果我在属性文件中设置status=debug
,并运行
java -Dlog4j.logger.dt.level=trace -Dlog4j2.logger.dt.level=trace -Dlogger.dt.level=trace
我从log4j得到以下日志(即com.ibm.data_triage仍然是INFO):
2023-05-11 18:18:59,512 main DEBUG Apache Log4j Core 2.17.2 initializing configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@d95dd310
2023-05-11 18:18:59,514 main DEBUG PluginManager 'Core' found 127 plugins
2023-05-11 18:18:59,515 main DEBUG PluginManager 'Level' found 0 plugins
2023-05-11 18:18:59,518 main DEBUG PluginManager 'Lookup' found 16 plugins
2023-05-11 18:18:59,519 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2023-05-11 18:18:59,529 main DEBUG PluginManager 'TypeConverter' found 26 plugins
2023-05-11 18:18:59,539 main DEBUG createAppenderRef(ref="STDOUT", level="null", Filter=null)
2023-05-11 18:18:59,539 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2023-05-11 18:18:59,550 main DEBUG LoggerConfig$Builder(additivity="null", level="INFO", levelAndRefs="null", name="com.ibm.data_triage", includeLocation="null", ={STDOUT}, ={}, Configuration, Filter=null)
2023-05-11 18:18:59,552 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2023-05-11 18:18:59,553 main DEBUG createAppenderRef(ref="STDOUT", level="null", Filter=null)
2023-05-11 18:18:59,553 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2023-05-11 18:18:59,556 main DEBUG LoggerConfig$RootLogger$Builder(additivity="null", level="WARN", levelAndRefs="null", includeLocation="null", ={STDOUT}, ={}, Configuration, Filter=null)
2023-05-11 18:18:59,556 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2023-05-11 18:18:59,558 main DEBUG createLoggers(={com.ibm.data_triage, root})
2023-05-11 18:18:59,559 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2023-05-11 18:18:59,561 main DEBUG PatternLayout$Builder(pattern="%d{yyyy-dd HH:mm:ss} %p %l - %m%n", PatternSelector=null, Configuration, Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2023-05-11 18:18:59,561 main DEBUG PluginManager 'Converter' found 45 plugins
2023-05-11 18:18:59,577 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2023-05-11 18:18:59,581 main DEBUG ConsoleAppender$Builder(target="null", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{yyyy-dd HH:mm:ss} %p %l - %m%n), name="STDOUT", Configuration, Filter=null, ={})
2023-05-11 18:18:59,586 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2023-05-11 18:18:59,587 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2023-05-11 18:18:59,588 main DEBUG createAppenders(={STDOUT})
2023-05-11 18:18:59,589 main DEBUG Configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@d95dd310 initialized
2023-05-11 18:18:59,589 main DEBUG Starting configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@d95dd310
2023-05-11 18:18:59,590 main DEBUG Started configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@d95dd310 OK.
2023-05-11 18:18:59,591 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2023-05-11 18:18:59,591 main DEBUG OutputStream closed
2023-05-11 18:18:59,592 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2023-05-11 18:18:59,592 main DEBUG Appender DefaultConsole-1 stopped with status true
2023-05-11 18:18:59,593 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@6fbdbc94 OK
2023-05-11 18:18:59,630 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be
2023-05-11 18:18:59,634 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be,component=StatusLogger
2023-05-11 18:18:59,637 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be,component=ContextSelector
2023-05-11 18:18:59,638 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be,component=Loggers,name=
2023-05-11 18:18:59,639 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be,component=Loggers,name=com.ibm.data_triage
2023-05-11 18:18:59,641 main DEBUG Registering MBean org.apache.logging.log4j2:type=ed8046be,component=Appenders,name=STDOUT
2023-05-11 18:18:59,645 main DEBUG org.apache.logging.log4j.core.util.SystemClock supports precise timestamps.
2023-05-11 18:18:59,646 main DEBUG Reconfiguration complete for context[name=ed8046be] at URI C:\Users\DavidWood\git\data-triage-4j\data-triage-core\target\classes\log4j2.properties (org.apache.logging.log4j.core.LoggerContext@1414d955) with optional ClassLoader: null
2023-05-11 18:18:59,647 main DEBUG Shutdown hook enabled. Registering a new one.
2023-05-11 18:18:59,653 main DEBUG LoggerContext[name=ed8046be, org.apache.logging.log4j.core.LoggerContext@1414d955] started OK.
是我期待太多了,还是有别的办法?
谢谢
1条答案
按热度按时间knsnq2tg1#
Java系统属性和
log4j2.properties
文件之间没有链接,因此您不能通过更改同名Java系统属性来更改配置文件中logger.dt.level
的值。但是,您可以通过在属性值中使用lookup来引入依赖项:
如果您想更进一步,则需要编写自己的
PropertiesConfigurationFactory
,使用高位(数字)顺序,用Java系统属性中的值覆盖属性文件。