Java Logging Frameworks and Tools

Logging information in your application is critical for understanding run-time behavior of any application, especially in cases when you encounter unexpected scenarios, errors or just for tracking certain activities.

As more and more companies move to the cloud, log analysis and log management tools and services are becoming more critical. Some of the tools such as Loggly, Logstash, Graylog, etc., help you to analyze and monitor logs.

Overall, logging is certainly a fundamental practice that provides significant benefits during the lifetime of the application.

Let’s look at some of Java logging frameworks and tools.

Apache Log4j 2

Apache Log4j 2 is an upgrade to Log4j that provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture. It provides asynchronous logging which significantly improves performance especially in multi-threaded applications.

We can include the maven dependency as follows:

<dependency>

    <groupId>org.apache.logging.log4j</groupId>

    <artifactId>log4j-api</artifactId>

    <version>2.9.1</version>

</dependency>

<dependency>

    <groupId>org.apache.logging.log4j</groupId>

    <artifactId>log4j-core</artifactId>

    <version>2.9.1</version>

</dependency>

Logback

Logback is an improved version of log4j. It is conceptually like log4j as both projects were founded by the same developer. If you are already familiar with log4j, you can pick up Logback faster.

Visit the official site to know about all of its benefits.

Include the maven dependency as follows:

<dependency>

    <groupId>ch.qos.logback</groupId>

    <artifactId>logback-classic</artifactId>

    <version>1.2.3</version>

</dependency>

tinylog

tinylog is a logging framework for Java and Android. The logger is static, so it is not necessary to create an instance of the logger before logging.

We can include maven dependency as follows:

<dependency>

    <groupId>org.tinylog</groupId>

    <artifactId>tinylog</artifactId>

    <version>1.2</version>

</dependency>

Logbook

Logbook is a Java library to enable complete request and response logging for various client and server-side technologies. It provides support for Servlet containers, Apache’s HTTP client, and other frameworks. It also has auto configuration for spring boot.

We can selectively add the dependencies to the project:

<dependency>

    <groupId>org.zalando</groupId>

    <artifactId>logbook-core</artifactId>

    <version>1.4.0</version>

</dependency>

<dependency>

    <groupId>org.zalando</groupId>

    <artifactId>logbook-servlet</artifactId>

    <version>1.4.0</version>

</dependency>

<dependency>

    <groupId>org.zalando</groupId>

    <artifactId>logbook-httpclient</artifactId>

    <version>1.4.0</version>

</dependency>

<dependency>

    <groupId>org.zalando</groupId>

    <artifactId>logbook-spring-boot-starter</artifactId>

    <version>1.4.0</version>

</dependency>

Conclusion

Logging is very important in application development, due to highly useful and actionable insights it brings during run-time of the application. Choose the framework as per your project needs and log wisely!