Search code examples
jbosslog4j2

log4j2 enabling debug for specific classes from environment variable


we are deploying a Java web application in JBoss 7.. 3 EAP. We need to enable set the debug level for some specific classes (i.e. org.mycompany.mypackage). As described here: https://logging.apache.org/log4j/2.x/manual/configuration.html just setting the environment variable

LOG4J_DEBUG

the log level is set to debug for whole system. We wonder if there is a way to do it for specific classes/packages Thank in advance r.


Solution

  • The LOG4J_DEBUG environment variable does not enable debug logging for your application. It enables debug logging for the internals of Log4j 2. You set the default logging level for your applicaton by specifying a level on the Root Logger.

    <Loggers>
      <Root leve="info">
        <Appender-Ref ref="Console"/>
      </Root>
    </Loggers>
    

    You could change this so that the default level can be set from an environment variable by doing:

    <Loggers>
      <Root leve="${env:DEFAULT_LEVEL:-INFO}">
        <Appender-Ref ref="Console"/>
      </Root>
    </Loggers>
    

    The way to set the logging level for specific classes or Packages is to use a log4j configuration file as described in that same section. If you want the levels to be configured via environment variables then you can do that by doing something like:

    <Logger name="com.mycorp.myservice.dao" level="${env:DAO_LEVEL:-WARN}"\>
    

    This will use a default value of WARN if the environment variable named DAO_LEVEL is not present.