Skip to content

Logging In Java

indiradoddi edited this page Apr 3, 2023 · 5 revisions

Introduction

Logging is a way to record events and messages in a Java application. The Java Logging API provides a standardized mechanism for logging information, such as error messages, debugging information, and performance data etc. Spring Boot supports all logger levels such as “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”. This allows you to control the amount of information that is logged, and make it easier to identify and resolve problems in your application. Spring Boot’s Logging Architecture

Spring Boot’s Logging Architecture

image

As you see, for internal APIs Spring Boot uses Java Commons Logging (JCL). For public APIs that are exposed to application programmers, Spring Boot uses SLF4J (Simple Logging Façade for Java) which allows programmers to choose different logging frameworks.

Currently, Spring Boot comes with libraries for Java Util Logging (JUL), Log4J2 and Logback. And Logback is used as the default logging framework. That means when using Spring Boot, you don’t have to specify any logging framework dependencies, as they are included by default.

Log Messages

Since Spring Boot uses SLF4J for public APIs, we can declare a logger using SLF4J API. Please find below the example:

image

Output of the above code:

image

As we see in the output, only logging messages written in the levels INFO, WARN and ERROR are outputted to the console. It is because the default log level used by Spring Boot is INFO.

To enable lower log levels i.e. DEBUG or TRACE, we need to specify the following entry in the application.properties file:

debug=true; trace=true;

Configure Log Level

Spring Boot also allows you to specify a specific log level for a specific logger name in the application.properties file. For example, the following line sets the log level to WARN for all loggers: logging.level.root=WARN

And the following line specifies log level DEBUG for only loggers in the package com.logging.utils: logging.level.com.logging.utils=DEBUG That means we can specify different log levels for different loggers, depending on the need.

Customize Log Pattern

To apply customizations to logback beyond those that can be achieved with application.properties, we will need to add a standard logback configuration file. We can add a logback.xml or logback-spring.xml file to the "src/main/resources" directory of your project. Spring Boot will automatically detect it and use it to configure the logging system. Here's an example of a basic "logback.xml" file:

image

The log pattern is defined within the element of the element. The %d, %thread, %level, %logger, and %msg are placeholders that will be replaced with the corresponding information when a log message is logged. Here's a breakdown of the placeholders used in the log pattern: %d{yyyy-MM-dd HH:mm:ss.SSS}: The date and time the log message was logged. [%thread]: The thread that the log message was logged from. %-5level: The log level of the message, left-aligned with a width of 5 characters. %logger{36}: The name of the logger that the log message was logged from, truncated to a maximum length of 36 characters. -%msg%n: The log message and a newline character.

Mapped Diagnostic Context (MDC) - Improved Java Logging

Mapped Diagnostic Context (MDC) provides a way to enrich log messages with information that could be unavailable in the scope where the logging actually occurs but that can be indeed useful to better track the execution of the program. Logback provides MDC as a built-in feature, allowing you to add key-value pairs to the MDC map and include them in log messages. Please find below the simple logback.xml which includes two MDC values, "id" and "requestId", in the log message format.

image

We can add MDC values by calling the MDC.put() method with the key-value pairs you want to add. For example:

image

This will generates the below output:

image

Clone this wiki locally