Log4j2

Aishwar Muthuraman
2 min readNov 1, 2020

--

Log4j2 is a logging framework in Java. You can use it in your application or library to emit logs.

A common pattern in Java is to have a Logging API that your application or library can be coded against to emit logs. And at runtime, you provide a logging implementation that actually does the log emission.

In the case of Log4j2 — they vend 2 libraries Log4j-api and Log4j-core. The Log4j-api provides the interface you can use for emitting logs. The Log4j-core library takes care of providing an implementation for those API calls.

When you configure Log4j in your application for logging, you specify the logging configuration in a configuration file. There are many different formats of configuration file that Log4j can accept. See here for details. XML files are one of the most common formats used for the configuration file. A sample one would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

There are 3 main entities to understand here:

  • Logger: The Logger element here represents the configuration for a Logger object. You can specify settings like level which control what level of logs should be emitted. You connect your loggers to specific appenders using AppenderRef.
  • Appender: this specifies the destination for your log events (the logs you emit). In the above example, we specify a Console appender; some other types of appenders are File appender, JDBC appender etc.
  • PatternLayout: this specifies how to format your log events when sending it to that destination

Within your Java code, you would write code that looks like this:

Logger logger = LogManager.getLogger(getClass());

This create a Logger object using the fully qualified name of the class as the logger name. The logger name is used to retrieve the appropriate configuration for the logger. Thus a logger instantiated in com.foo.Bar (in Java code) with the above construct will use a configuration that matches that name com.foo.Bar (in the configuration file). This way we can use the configuration file to control log emitting levels for a particular class.

To make log configuration easier, Log Configurations are hierarchical. If a configuration was not defined for com.foo.Bar, the logger in that class will use fallback to the following configurations: com.foo.Bar, com.foo, com and ultimately the root logger configuration. This is what is represented by the Root element in the XML configuration file above. This is the only required logger configuration.

--

--

Aishwar Muthuraman

Adventuring through life. Stories of software development, engineering, fun, and reflection.