Skip to content

Commit 1ed3dea

Browse files
committed
添加日志打印
1 parent 5c2bf82 commit 1ed3dea

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

pom.xml

+34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<java.version>1.8</java.version>
2323
<spring.version>2.2.1.RELEASE</spring.version>
2424
<junit.version>4.12</junit.version>
25+
<log4j.version>2.12.1</log4j.version>
26+
<slf4j.version>1.7.25</slf4j.version>
2527
</properties>
2628

2729
<dependencies>
@@ -31,6 +33,12 @@
3133
<artifactId>spring-boot-starter-test</artifactId>
3234
<scope>test</scope>
3335
<version>${spring.version}</version>
36+
<exclusions>
37+
<exclusion>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-logging</artifactId>
40+
</exclusion>
41+
</exclusions>
3442
</dependency>
3543
<dependency>
3644
<groupId>org.springframework.cloud</groupId>
@@ -44,6 +52,32 @@
4452
<version>${junit.version}</version>
4553
<scope>test</scope>
4654
</dependency>
55+
<!-- 日志打印 -->
56+
<dependency>
57+
<groupId>org.slf4j</groupId>
58+
<artifactId>slf4j-api</artifactId>
59+
<version>${slf4j.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.apache.logging.log4j</groupId>
63+
<artifactId>log4j-slf4j-impl</artifactId>
64+
<version>${log4j.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.apache.logging.log4j</groupId>
68+
<artifactId>log4j-api</artifactId>
69+
<version>${log4j.version}</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.logging.log4j</groupId>
73+
<artifactId>log4j-core</artifactId>
74+
<version>${log4j.version}</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.lmax</groupId>
78+
<artifactId>disruptor</artifactId>
79+
<version>3.4.2</version>
80+
</dependency>
4781
</dependencies>
4882

4983
<build>

src/main/resources/application.yml

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ spring:
1717
# username和password必须配对使用,公开库可以不用填写
1818
username:
1919
password:
20+
logging:
21+
config: classpath:log4j2.xml

src/main/resources/log4j2.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<!--变量配置-->
4+
<Properties>
5+
<!-- 指定项目名称 -->
6+
<property name="appName" value="coding-config-server" />
7+
<!-- 指定日志存储的路径 -->
8+
<property name="loggingPath" value="${env:LOG_HOME:-/appdata/logs/}" />
9+
<!-- 格式化输出:%d表示日期,%t表示线程名,%-5level表示级别从左显示5个字符宽度 %m是日志消息,%n是换行符-->
10+
<property name="loggingPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [${hostName}] [${appName}] [%-5level] [%t] [%c{1.}] :) %m%n" />
11+
</Properties>
12+
13+
<Appenders>
14+
<!-- 打印到控制台以便调试 -->
15+
<Console name="Console" target="SYSTEM_OUT">
16+
<PatternLayout pattern="${loggingPattern}"/>
17+
</Console>
18+
19+
<!-- 打印到文档中以便存档 -->
20+
<RollingFile name="LoggingFile" fileName="${loggingPath}/${appName}.log" filePattern="${loggingPath}/${appName}_%d{yyyy-MM-dd}_%i.log" bufferSize="4194304">
21+
<PatternLayout pattern="${loggingPattern}"/>
22+
<Policies>
23+
<!--指定当文件体积大于size指定的值时,触发Rolling-->
24+
<SizeBasedTriggeringPolicy size="20MB" />
25+
</Policies>
26+
<!-- 最大保留10个文件 -->
27+
<DefaultRolloverStrategy max="4" />
28+
</RollingFile>
29+
</Appenders>
30+
31+
<Loggers>
32+
<!--日志级别优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
33+
<!--AsyncRoot 异步记录日志 -->
34+
<AsyncRoot level="info">
35+
<AppenderRef ref="Console"/>
36+
<AppenderRef ref="LoggingFile"/>
37+
</AsyncRoot>
38+
</Loggers>
39+
40+
</Configuration>

0 commit comments

Comments
 (0)