Technical architectural analysis, practical configuration manifests, and deployment instructions for Spring Boot logback-spring.xml: Rolling Files.
There are various logging libraries, as shown in the picture in Spring Boot 2.0
Java Util Logging, Log4j 2, Logback Can be used by default.
To make it easier to switch to another logging library, lombok must be added to the dependency to use Slf4j with the Facade pattern.
If you apply the @Slf4j annotation provided by Lombok, you can easily check the log with just log.debug().
In Spring Boot, logback is the basic logging framework and includes the dependent slf4j api and bridge module, so there is no need to add modules related to log processing.
Springboot includes logback by default. Therefore, when you call and execute the main() function of Springboot’s *Application.java file, this is what appears in the IDE’s console window.
https://linkeverything.github.io/springboot/spring-logging/
If you look for logback related settings, you will see logback.xml vs logback-spring.xml
After the Web Application starts, search logback.xml in the classpath and apply the environment settings.
At this time, the values existing in application.properties cannot be loaded because Spring is not running.
In logback-spring.xml, the values in application.properties can be loaded after Spring is running.
- There is an implementation in spring-boot-starter-logging in spring-boot-start-web.
- There are three main configuration elements required for logging using Logback: Logger, Appender, and Encoder.
For Spring or general Java programs, the logback.xml file is created in the resources directory and referenced, but for Spring Boot, choose one of the three methods below.
- Set in application.properties
- Setting in resources/logback-spring.xml
- Set in resources/logback.xml
Log level
TRACE \< DEBUG \< INFO \< WARN \< ERROR
https://yjh5369.tistory.com/entry/Spring-Boot-Log-Configuration-Logback
https://goddaehee.tistory.com/206?category=367461
https://programmer93.tistory.com/46
[ Logging Pattern ]
%-5level: log level, -5 is a fixed width value (5 characters) of output (INFO, ERROR, DEBUG, etc.)
%d{date format format}: Log recording time: %d{yyyy-MM-dd HH\:mm\:ss} can be used.
%thread: Current thread name
%F: File name that caused logging
%M: Method name that caused logging
%logger{length}: Logger name can be abbreviated. length is the maximum number of digits (0 = unlimited)
%line: Line number where logging occurred%msg: โ Log message
%n: line break (new line)
There are many other things besides this. ( http://logback.qos.ch/manual/layouts.html )
Console color
logback-spring.xml color application example
<!-- ์ด ์์ฑ ์ ์ธ๊ฑฐ์ -->
// <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){green} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
// <property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-default}"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %green(%-5level) %logger{35} %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
// %green(%-5level) %logger{35} %cyan(%logger{15}) ์๋ฐ ์์ผ๋ก ์๊น์ ์ค ์ ์๋ค.
- You can use
%highlightto give a color according to the log level. %black,%red,%green,%yellow,%blue,%magenta,%cyan,%white,%gray,%boldRed,%boldGreen, You can also use%boldYellow,%boldBlue,%boldMagenta,%boldCyan, and%boldWhite.- The scope of application is () and can be used like this:
%highlight([%-5level]).
9. JANSI on Windows
While Unix-based operating systems such as Linux and Mac OS X support ANSI color codes by default, on a Windows console, everything will be sadly monochromatic.
Windows can obtain ANSI colors through a library called JANSI.
We should pay attention to the possible class loading drawbacks, though.
We must import and explicitly activate it in the configuration as follows:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<!-- more stuff -->
</configuration>
https://www.baeldung.com/spring-boot-logging
log4j2-spring.xml
https://hermeslog.tistory.com/451
Log4JDBC
https://congsong.tistory.com/23
spyLogDelegatorName does not allow to valid
In my case, an error occurred because spaces were applied to the log4jdbc.log4j2.properties file.
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
https://hermeslog.tistory.com/454?category=302344
<?xml version="1.0" encoding="UTF-8"?>
<!-- 60์ด๋ง๋ค ์ค์ ํ์ผ์ ๋ณ๊ฒฝ์ ํ์ธ ํ์ฌ ๋ณ๊ฒฝ์ ๊ฐฑ์ -->
<configuration scan="true" scanPeriod="60 seconds">
<!--๋ก๊ทธ ํ์ผ ์ ์ฅ ์์น -->
<springProfile name="dev">
<property name="LOGS_PATH" value="./logs" />
</springProfile>
<!-- ์ด ์์ฑ ์ ์ธ๊ฑฐ์ -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){green} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-default}" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %green(%-5level) %logger{35} %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<appender name="DAILY_ROLLING_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS_PATH}/logback.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level%logger{35} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGS_PATH}/logback.%d{yyyy-MM-dd}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>5MB</maxFileSize>
<!-- kb, mb, gb -->
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<logger name="net.lunalabs.central" level="INFO">
<appender-ref ref="DAILY_ROLLING_FILE_APPENDER" />
</logger>
<!-- log4jdbc ์ต์
์ค์ -->
<logger name="jdbc" level="OFF" /> <!-- ์ปค๋ฅ์
open close ์ด๋ฒคํธ๋ฅผ ๋ก๊ทธ๋ก ๋จ๊ธด๋ค. -->
<logger name="jdbc.connection" level="OFF" /> <!-- SQL๋ฌธ๋ง์ ๋ก๊ทธ๋ก ๋จ๊ธฐ๋ฉฐ, PreparedStatement์ผ ๊ฒฝ์ฐ ๊ด๋ จ๋ argument ๊ฐ์ผ๋ก ๋์ฒด๋ SQL๋ฌธ์ด ๋ณด์ฌ์ง๋ค. -->
<logger name="jdbc.sqlonly" level="OFF" /> <!-- SQL๋ฌธ๊ณผ ํด๋น SQL์ ์คํ์ํค๋๋ฐ ์ํ๋ ์๊ฐ ์ ๋ณด(milliseconds)๋ฅผ ํฌํจํ๋ค. -->
<logger name="jdbc.sqltiming" level="DEBUG" /> <!-- ResultSet์ ์ ์ธํ ๋ชจ๋ JDBC ํธ์ถ ์ ๋ณด๋ฅผ ๋ก๊ทธ๋ก ๋จ๊ธด๋ค. ๋ง์ ์์ ๋ก๊ทธ๊ฐ ์์ฑ๋๋ฏ๋ก ํน๋ณํ JDBC ๋ฌธ์ ๋ฅผ ์ถ์ ํด์ผ ํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ๋ ์ฌ์ฉ์ ๊ถ์ฅํ์ง ์๋๋ค. -->
<logger name="jdbc.audit" level="OFF" /> <!-- ResultSet์ ํฌํจํ ๋ชจ๋ JDBC ํธ์ถ ์ ๋ณด๋ฅผ ๋ก๊ทธ๋ก ๋จ๊ธฐ๋ฏ๋ก ๋งค์ฐ ๋ฐฉ๋ํ ์์ ๋ก๊ทธ๊ฐ ์์ฑ๋๋ค. -->
<logger name="jdbc.resultset" level="OFF" /> <!-- SQL ๊ฒฐ๊ณผ ์กฐํ๋ ๋ฐ์ดํฐ์ table์ ๋ก๊ทธ๋ก ๋จ๊ธด๋ค. -->
<logger name="jdbc.resultsettable" level="OFF" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
https://exhibitlove.tistory.com/302
Logback settings
Level The higher the level to the right in the following order: TRACE โ DEBUG โ INFO โ WARN โ ERROR. Only logs above the output level are output.
Appendar
An object that handles the function of recording logs for each event. Set the output location and output format of the log. There are three basic appenders in the logback-core module.
- ConsoleAppender: Output log to console
- FileAppender: Write log to specified file
- RollingFileAppender: Inherits FileAppender. You can set the date and capacity so that logs are recorded in different files according to patterns.




Leave a Reply