SpringBoot 使用 logbak

依赖版本

Spring Boot:1.5.18

参考文档

配置说明

  • spring-boot-1.5.18.RELEASE.jar包下 org.springframework.boot.logging.logback中有logback的默认配置

  • logback-classic-1.1.11.jar包下ch.qos.logback.classic.db.script中有数据库初始化脚本

POM配置

pom.xml

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mytest</groupId>
<artifactId>logback</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<mysql.version>5.1.41</mysql.version>
<druid-starter.version>1.1.10</druid-starter.version>
</properties>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
</parent>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--&lt;!&ndash; 热部署 &ndash;&gt;-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<!-- springboot集成druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-starter.version}</version>
</dependency>
<!-- mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- SpringBoot 单元测试支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>

<!-- janino 库,logback的JaninoEventEvaluator依赖此库-->
<!--<dependency>-->
<!--<groupId>org.codehaus.janino</groupId>-->
<!--<artifactId>janino</artifactId>-->
<!--<version>3.0.11</version>-->
<!--</dependency>-->
</dependencies>

<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

Spring Boot配置

application.properties

1
2
3
4
#
# ${user.home}为windows系统的用户目录
# ${home}为Linux系统中的用户目录
logging.path=${user.home}/logs/

logback配置

日志写入文件

file-appender.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<included>
<property name="LOG_FILE_NAME" value="test"></property>
<property name="LOG_FILE" value="${LOG_PATH}/${LOG_FILE_NAME}.log"/>
<!-- 写入文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/logback.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
</appender>
</included>

日志写入数据库

database-appender.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8" ?>
<included>
<!-- 写入数据库-->
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="com.alibaba.druid.pool.DruidDataSource">
<driverClassName>com.mysql.jdbc.Driver</driverClassName>
<url>jdbc:mysql://127.0.0.1/test?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;useSSL=false</url>
<username>root</username>
<password>root</password>
</dataSource>
</connectionSource>
</appender>

</included>

发送日志邮件

mail-appender.xml

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
<?xml version="1.0" encoding="UTF-8" ?>
<included>
<!-- 邮件发送 -->
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<!-- SMTPAppender 的默认级别为Error -->
<!-- 当前版本SMTPAppender使用Filter定义日志等级无效,可以采用evaluator定义-->
<!-- 使用JaninoEventEvaluator需添加janino库-->
<!--<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">-->
<!--<expression>return level >= WARN;</expression>-->
<!--</evaluator>-->
<smtpHost>smtp.qq.com</smtpHost>
<smtpPort>465</smtpPort>
<username>123456789@qq.com</username>
<password>密码</password>
<!--<asynchronousSending>true</asynchronousSending>-->
<SSL>true</SSL>
<to>12345@qq.com</to>
<from>123456789@qq.com</from>
<subject>%logger{20} - %m</subject>
<!-- html格式-->
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<Pattern>%d%p%t%c%L%m%ex</Pattern>
</layout>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<!-- 每个电子邮件发送日志条目 -->
<bufferSize>1</bufferSize>
</cyclicBufferTracker>
</appender>
</included>

结合Spring Boot

logback-spring.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 引入springboot的基本配置-->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!-- 引入springboot的console配置-->
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<!-- 当spring.profiles.active为dev时激活 -->
<!-- 参考‘Spring Boot:boot-features-logging’中的‘Profile-specific configuration’ -->
<springProfile name="dev">
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</springProfile>

<springProfile name="prod">
<include resource="database-appender.xml" />
<include resource="file-appender.xml" />
<include resource="mail-appender.xml" />
<root level="INFO">
<!--<appender-ref ref="CONSOLE" />-->
<appender-ref ref="FILE" />
<appender-ref ref="DB" />
<appender-ref ref="EMAIL" />
</root>
</springProfile>
</configuration>
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~