Log4j2 setting log levels

12,452

Solution 1

Level intLevel
OFF   0
FATAL 100
ERROR 200
WARN  300
INFO  400
DEBUG 500
TRACE 600
ALL   Integer.MAX_VALUE

https://logging.apache.org/log4j/2.x/manual/customloglevels.html

A log4j logger will log all events under its threshold, so if is set to ALL it will log every event since it uses the max value for its threshold.

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile

This is defining a new rolling file appender named RollingFile. A rolling file appender is an appender that can, for example, grow to a fixed size and then keep adding new entries while removing the oldest entries. They can also be time-based etc.

logger.rolling.name = com.test.app
logger.rolling.level = ALL
logger.rolling.appenderRef.rolling.ref = RollingFile

This is telling log4j to send any events from the logger named com.test.app to the aforementioned RollingFile appender. log4j will not filter out any events since the level for this logger is set to ALL. "rolling" as in "logger.rolling" is just the identifier for the logger. This is necessary because the properties file is unstructured so you need a way to distinguish which lines go together. Using the XML configuration eliminates that need

rootLogger.level = info

Any events created by other loggers that are not defined in the properties will be filtered and only INFO or below will be logged.

The documentation is here: https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties

To answer your question, it depends on how the loggers are created within the application. The loggers are actually created within java, and that is where their name is assigned. The properties file only instructs log4j on how to handle each logger. You will need to add the logger to the properties file using the name defined in the class. If the class itself was used to create the logger, as apache recommends, then the name will always be the fully qualified name of the class. For example:

 logger.secondclass.name = com.test.AnotherClass
 logger.secondclass.level = DEBUG
 logger.secondclass.appenderRef.rolling.ref = RollingFile

Now any DEBUG or below events created by the com.test.AnotherClass will also be sent to the RollingFile appender.

All of this is explained here: https://logging.apache.org/log4j/2.x/manual/architecture.html

Solution 2

Lets understand step by step:

appenders = console,rolling

In appenders, we write the names where we want to see our logs. Suppose, we want to see the logs on console and a file (which is of type rolling)

NOTE: These appender names are custom names. We can give any name for the appenders. for eg: The appenders could also have been:

appenders = consoleOutput,myrollingFile

Next, we define some properties for each of the appender. These properties are like

  • how log messages should be display.
  • What will be the pattern in which it will be shown, etc.

For this , we use the following format:

appender.<appender-name>.<property-name>=<property-value>

For eg:

appender.rolling.type = File

This defines that appender of rolling name will be logging its logs in the File type.

Next, we define the loggers

loggers = rolling

NOTE: These loggers names are custom names. We can give any name for the loggers.

The loggers usually describes the following things for logging:

  • for what packages i.e package name?
  • what should be the level of the logs?
  • Where will we log these specific type of logs? on console? on a file?

For this , we use the following format:

logger.<logger-name>.<logger-property>=<property-value>

logger.rolling.name = com.my.package.name  
logger.rolling.level = debug  
logger.rolling.additivity = true  
logger.rolling.appenderRef.rolling.ref = File 
Share:
12,452
Richard
Author by

Richard

Updated on June 12, 2022

Comments

  • Richard
    Richard almost 2 years

    I am using log4j2 as my logging utility in my spring app. I want to set the log level for specific libraries/packages to something different than the root. For example, I want org.springframework to INFO and com.google to be WARN. I found this in the log4j2.properties:

    appender.rolling.type = RollingFile
    appender.rolling.name = RollingFile
    logger.rolling.name = com.test.app
    logger.rolling.level = ALL
    logger.rolling.appenderRef.rolling.ref = RollingFile
    
    rootLogger.level = info
    

    I don't understand what logger.rolling means? I keep finding it in the log4j2 documentation but there's no explanation of what that is or what ALL means.

    How do I add log levels for specific packages and what is this rolling level stuff?