I am using log4j to manage logging in a Clojure application. I would like to be able to use a system variable to set the log level for a single package within the log4j.properties file. So, for instance, here's my base properties file:
log4j.rootLogger=INFO,console,sentry
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss,SSSZ} %-5p [%t] %-4r %c %x - %m%n
log4j.logger.company.logging.logger=DEBUG
This sets the logging level to DEBUG in company.logging.logger
(where the specific logging code is located. However, I don't want that - I want it to be DEBUG on local but INFO on production (AWS). So, I use an environment variable:
log4j.rootLogger=INFO,console,sentry
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss,SSSZ} %-5p [%t] %-4r %c %x - %m%n
log4j.logger.company.logging.logger=${env:LOG_LEVEL}
This doesn't work. LOG_LEVEL
in this instance is set to DEBUG
and I can confirm that it would otherwise work by using the environment variable to set the root
logging level to DEBUG.
So, for instance:
log4j.rootLogger=${env:LOG_LEVEL},console,sentry
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss,SSSZ} %-5p [%t] %-4r %c %x - %m%n
log4j.logger.company.logging.logger=${env:LOG_LEVEL}
This sets the entire application's logging level to DEBUG
. This isn't desirable: it makes the application logs so verbose as to be unusable.
Question is this:
Is it possible to use environment variables to set a single package's log level in log4j? Is this a bug in the L4J ecosystem? Or is this intended behavior and there's a different way we should solve this problem?