Spring Boot provides several logging options, and one of the most commonly used libraries for logging is Logback. Logback is the default logging framework when you use Spring Boot starters.
Eang Sopheaktra
April 20 2024 12:46 am
This example explained about spring boot 3 with Logback which's come with spring boot starter:
Logging plays a crucial role in computer systems and software applications. Let’s explore why it’s so important:
Debugging and Troubleshooting:
Monitoring and Alerting:
Security and Auditing:
Forensics and Incident Response:
Performance Optimization:
Capacity Planning and Scaling:
Compliance and Legal Requirements:
Application Insights and Business Intelligence:
For sure it's require and you can't build application without logging because it'll make your life stucking with problems, issues, bugs on production. And
Answer me if you are in these above question. No worry I have been do like above question before when I am just start development and the first start alway like this hahahaha.
These question above will not happend if your organization is restrict to protect risk on something change or deployment. I meant you will not allow to access to production, db production and everything that live for client using. The one way is asking for log file or db and find the problems that is the best solution.
Sure because spring boot is auto configuration and already implement default logging for us like below:
spring-jcl
. However, Spring Boot 2.x already provides it through starters (e.g., spring-boot-starter-web
).--debug
or --trace
arguments on the command line when running your application.TRACE:
DEBUG:
INFO:
WARN:
ERROR:
FATAL (not always present in all logging frameworks):
The fully fledged server uses the following:
There are a number of third-party dependencies used in the project. Browse the Maven pom.xml file for details of libraries and versions used.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
logback-spring.xml
or logback.xml
in the classpath.-spring
variant (e.g., logback-spring.xml
) over the plain ones.logback-spring.xml
configuration with condition of spring boot profiles:<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="name" source="spring.application.name"/>
<property name="LOG_FILE" value="logs/${name:-LogFile}" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE:-}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE:-}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<springProfile name="!prod">
<maxFileSize>50MB</maxFileSize>
</springProfile>
<springProfile name="prod">
<maxFileSize>500MB</maxFileSize>
</springProfile>
<!-- keep 7 days' worth of history capped at 1GB total size -->
<maxHistory>7</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">-->
<!-- <fileNamePattern>${LOG_FILE:-}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>-->
<!-- <minIndex>1</minIndex>-->
<!-- <maxIndex>10</maxIndex>-->
<!-- </rollingPolicy>-->
<!-- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
<!-- <maxFileSize>1KB</maxFileSize>-->
<!-- </triggeringPolicy>-->
<encoder>
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread, ${name:-}, %X{traceId:--}, %X{spanId:--}] %-5level %logger{36}.%M - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread, ${name:-}, %X{traceId:--}, %X{spanId:--}]) %highlight(%-5level) %logger{36}.%M - %msg%n
</pattern>
</encoder>
</appender>
<logger name="com.tra21.logback" level="debug" additivity="false">
<appender-ref ref="STDOUT"/>
<springProfile name="prod">
<appender-ref ref="FILE"/>
</springProfile>
</logger>
<!-- <logger name="org.hibernate.SQL" level="debug" additivity="false">-->
<!-- <appender-ref ref="STDOUT"/>-->
<!-- <springProfile name="!prod">-->
<!-- <appender-ref ref="FILE"/>-->
<!-- </springProfile>-->
<!-- </logger>-->
<!-- <logger name="com.zaxxer.hikari" level="debug" additivity="false">-->
<!-- <appender-ref ref="STDOUT"/>-->
<!-- <springProfile name="prod">-->
<!-- <appender-ref ref="FILE"/>-->
<!-- </springProfile>-->
<!-- </logger>-->
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
After that logback-spring.xml is create and patse the above code you will strange on my traceId and spanId. You will know about new update on spring boot will auto have "correlation ID" for all the logs relating to one HTTP transaction as long as micrometer tracing is enabled so that why my above dependencies management has micrometer. Also I'm create controller for testing with my log and check how its work.
package com.tra21.logback.controllers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/logs")
@Slf4j
public class LogController {
@GetMapping("/info")
public boolean logInfo(){
log.info("log-info with this message: {}", "Information Message");
return true;
}
@GetMapping("/warn")
public boolean logWarn(){
log.warn("log-warn with this message: {}", "Warn Message");
return true;
}
@GetMapping("/error")
public boolean logError(){
log.error("log-error with this message: {}", "Error Message");
return true;
}
@GetMapping("/debug")
public boolean logDebug(){
log.debug("log-debug with this message: {}", "Debug Message");
return true;
}
@GetMapping("/trace")
public boolean logTrace(){
log.trace("log-trace with this message: {}", "Trace Message");
return true;
}
}
You will need:
Clone the project and use Maven to build the server
$ mvn clean install
Download the source code for the sample application implementing logging on application. Also you will learn about log, its important, spring boot profiles configuration with logback, traceId and spanId with micrometer and some logback xml syntax and real example.