1

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?

EricBoersma
  • 1,019
  • 1
  • 8
  • 19
  • That seems strange, if the environment variable is working for the root logger I can't see a reason why it wouldn't work for a more specific logger. Can you confirm that the root logger is actually been set to particularly log level via the environment variable and it isn't just defaulting to DEBUG for whatever reason? I'm not sure on the particulars of l4j but you could be seeing a false positive. – Astronought May 21 '19 at 18:59

1 Answers1

0

Since you are using Lo4j 1.x APIs, you could programmatically set the log level of one or more Java packages to the value obtained from an environment variable which you'll set to a default value during development and to a different value in Production

See https://stackoverflow.com/a/4598829/483566 for an example of how to implement this. One of the comments in this link suggests the following:

LogManager.getLogger(Class.forName("org.hibernate.util.JDBCExceptionReporter")).setLevel(Level.FATAL);

... which should be easy to translate to Clojure.

Note: You seem to be using Logj 1.x which is end-of-life. You should be using Log4j2 or something like tools.logging.

Denis Fuenzalida
  • 3,271
  • 1
  • 17
  • 22