0

I would like to have a single log4j2 properties file delivered in my jar, but override its settings when starting the app. For example, the default level might be warn, but when debugging I'd like to change the log level to debug, for example. I am aware of the log4j.configurationFile system property, but that is a sledgehammer for this problem. I'm using log4j 2.17.2 and Java 11 and I'm not doing any special log4j initialization other than LogManager.getLogger(). My properties file is as follows:

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

Can I then do something like this?

java -Dlogger.dt.level=trace

or

java -Dlog4j.logger.dt.level=trace

Neither seem to work. I've also tried the solution from AbuNassar in How do I set log4j level on the command line?, but to no avail.

If I set status=debug in the properties file, and run

java -Dlog4j.logger.dt.level=trace  -Dlog4j2.logger.dt.level=trace -Dlogger.dt.level=trace 

I get the following logging from log4j (i.e., com.ibm.data_triage is still 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.

Am I expecting too much or is there another way?

Thanks.

David Wood
  • 434
  • 1
  • 5
  • 14

1 Answers1

0

There is no link between Java system properties and your log4j2.properties file, so you can not change the value of logger.dt.level in your configuration file by changing the homonymous Java system property.

You can however introduce a dependency, by using a lookup in the property value:

# Fallback value for the `logger.dt.level` system property
property.logger.dt.level = INFO
logger.dt.level = ${sys:logger.dt.level}

If you want to go further you'll need to write your own PropertiesConfigurationFactory with a high (numerical) order that overwrites the properties file with the values from Java system properties.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43