Spring Boot logback-spring.xml: Rolling Files

๐Ÿ’ก Spring Boot logback-spring.xml: Rolling Files โ€” Executive Summary

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

xml
<!-- ์ด ์†์„ฑ ์•ˆ ์“ธ๊ฑฐ์ž„ -->
//	<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 %highlight to 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:

xml
<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.

txt
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

https://hermeslog.tistory.com/454?category=302344

xml
<?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.

  1. ConsoleAppender: Output log to console
  2. FileAppender: Write log to specified file
  3. RollingFileAppender: Inherits FileAppender. You can set the date and capacity so that logs are recorded in different files according to patterns.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *