The Basic Settings of LogBack

In Java (Spring Boot) applications, the LogBack logging framework is often used to log, or it can be simplified with @Slf4j annotations in Lombok. To customize the output of the log, a configuration file named logback.xml is typically configured in the project.

pom.xml 文件

In the most basic case, a dependency library is required

1
2
3
4
5
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>

If used with lombok, you can define two dependent libraries

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Lombok and Log -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<!-- End: Lombok and Log -->

Configuration in logback.xml

In maven projects, logback.xml files are typically placed in the project’s src/main/resources directory. Here are some basics to make it easy for you to quickly build your own project log configuration

The most basic configuration:

1
2
3
4
5
6
7
8
9
10
11
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</layout>
</appender>
<root>
<level value="debug"/>
<appender-ref ref="stdout"/>
</root>
</configuration>

In the most basic configuration, we simply output the logs to the screen.

If we need to log the log in a local file, we usually need to add the following configuration information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<substitutionProperty name="logbase" value="${user.dir}/logs/ "/>
<!-- File output log (file size policy for file output, more than the specified size to file backup) -->
<appender name="logfile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<File>${logbase}%d{yyyy-MM-dd}.log</File>
<FileNamePattern>${logbase}.%d{yyyy-MM-dd}.log.zip
</FileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%level%thread%10logger%file%line%msg</pattern>
</layout>
</appender>

In the configuration, a variable named logbase is first declared, indicating the directory where the log files are stored. To prevent a single log file from being too large, a rollover mode of log files is declared.

After adding the file configuration, the full logback .xml as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
</layout>
</appender>

<substitutionProperty name="logbase" value="${user.dir}/logs/ "/>
<appender name="logfile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<File>${logbase}%d{yyyy-MM-dd}.log</File>
<FileNamePattern>${logbase}.%d{yyyy-MM-dd}.log.zip
</FileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%level%thread%10logger%file%line%msg</pattern>
</layout>
</appender>

<root>
<level value="debug"/>
<appender-ref ref="stdout"/>
<appender-ref ref="logfile"/>
</root>
</configuration>

Of course, we can also configure the log to be written to the database, and the following shows the configuration when storing the mySQL database as the log:

1
2
3
4
5
6
7
8
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://localhost:3306/logs</url>
<user>logUser</user>
<password>123456</password>
</connectionSource>
</appender>

本文标题:The Basic Settings of LogBack

文章作者:Morning Star

发布时间:2021年10月11日 - 12:10

最后更新:2022年01月08日 - 12:01

原始链接:https://www.mls-tech.info/java/java-logback-basic-setting-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。