Terpene

  • Comment: Written in part as a how-to guide? Poorly referenced and appears to contain a lot of WP:OR. — microbiologyMarcus [petri dish·growths] 16:25, 11 April 2024 (UTC)

logback.[1] is a Java logging framework[2][3]. It is the successor[4] of log4j 1.x and is very similar conceptually to log4j 1.x as both frameworks were developed by the same author.

The main difference between log4j 1.x and logback is that logback implements the SLF4J API natively, including support for the fluent API. In addition, logback has a larger battery of tests and more extensive documentation.[5] According to mvnrepository, logback the most popular Java logging framework.[3]

It should be noted that compared to log4j 1.x logback internals have been re-written to perform about ten times faster on certain critical execution paths. Not only are logback components faster, they have a smaller memory footprint as well. Compared to log4j 2.x, logback performs significantly better, even in the case of asynchronous logging and using only packages provided by the JDK.

The Spring Boot framework supports logback as its default logging system. Spring boot also support logback-access logging[6] for HTTP-access logs which are distinct from application logs.

Logback ships with a plethora of appenders, i.e. logging destinations, as well formatting features. This flexibility is highly valuable during problem diagnosis[7]

Documentaiton

There are dozens of articles and hundreds of stack overflow entries[8] describing logback. Also noteworthy is the logback manual which is quite exhaustive.

Modules

Logback ships with two modules. The logback-classic module which is the equivalent of log4j 1.x. The logback-access module can be integrated with web-container to log HTTP traffic. The logback-core module provides common functionality required by the previous two modules.

As of march 2024 and logback version 1.5.x, logback-access has moved to a separate github repository.

Dependencies

For Maven users, you can declare logback-classic as a dependency in the pom.xml file of your project.

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.4</version>
</dependency>

The transitive dependencies logback-core and slf4j-api will pulled in by Maven automatically.

Architecture of logback-classic

Logback-classic is based on logback-core and natively implements the SLF4J API. Logging is initiated by calling the printing methods of a logger[9] instance.

logger.debug("Hello world");

Loggers are obtained by calling the getLogger()[10] method of LoggerFactory[11] class.

Logger logger = LoggerFactory.getLogger(HelloWorld.class);

Configuration

Logback offers powerful configuration options which are too numerous to detail here.

However, here is a sample configuration file that logs to the console.

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Here is another configuration file telling logback to write to a file and to roll it over or archive it at midnight. Moreover, only 30 days of history are kept with a maximum size of 30 MB. It should be mentioned that in case of I/O failures, logback will recover gracefully. Thus, if a file server fails temporarily, you no longer need to restart your application just to get logging working again.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history capped at 3GB total size -->
      <maxHistory>30</maxHistory>
      <totalSizeCap>3GB</totalSizeCap>

    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Logback-tyler

For certain use cases, it is worthwhile to work directly with configuration files written in Java. The logback-tyler project will translate logback.xml file to it's equivalent but written in Java. The various examples in the logback manual offer a "Tyler" tab allowing you to experiment with logback-tyler configuration files.

References

  1. ^ "Logback Home". logback.qos.ch. Retrieved 2022-02-22.
  2. ^ "A Guide to Java Logging with Logback | Better Stack Community". betterstack.com. Retrieved 2024-04-11.
  3. ^ a b Goebelbecker, Eric (2018-05-08). "A Guide To Logback | Baeldung". www.baeldung.com. Retrieved 2022-08-05.
  4. ^ Paraschiv, Eugen (2017-08-07). "Solving Your Logging Problems with Logback". Stackify. Retrieved 2022-08-05.
  5. ^ "Logback Manual". logback.qos.ch. Retrieved 2022-08-05.
  6. ^ Ramallo, Facundo (2023-12-22). "Understanding Logback". Medium. Retrieved 2024-04-11.
  7. ^ Kuć, Rafal (2021-03-12). "Logback Tutorial: Configuration Example for Java Application Logging". Sematext. Retrieved 2024-04-11.
  8. ^ "Human verification". Stack Overflow. Retrieved 2024-04-11.
  9. ^ "Logger (Logback-Parent 1.3.0-alpha15 API)". logback.qos.ch. Retrieved 2022-08-05.
  10. ^ "LoggerFactory (SLF4J 2.0.0-alpha7 API)". www.slf4j.org. Retrieved 2022-08-05.
  11. ^ "LoggerFactory (SLF4J 2.0.0-alpha7 API)". www.slf4j.org. Retrieved 2022-08-05.

Leave a Reply