首先在pom.xml中加入以下俩个配置;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
然后在resource目录下新增log4j2.yml文件(log4j已停止更新了,这里就不在使用了)
Configuration:
status: warn
monitorInterval: 30
Appenders:
Console: #输出到控制台
name: CONSOLE #Appender命名
target: SYSTEM_OUT
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
RollingFile: # 输出到文件,超过256MB归档
- name: ROLLING_FILE
ignoreExceptions: false
fileName: logs/qisheng.log # 这个是日志文件路径名称
filePattern: "/logs/$${date:yyyy-MM}/qisheng -%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "256 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
Logger: #单独设置某些包的输出级别
- name: com.qisheng.mgjcrm #这个名称一定要是你项目中所存在的包名
additivity: false #去除重复的log
level: trace
AppenderRef:
- ref: CONSOLE #复数加上-
- ref: ROLLING_FILE #复数加上
最后在application.yml中指定上述配置的路径:
logging:
level:
com.qisheng.mgjcrm.dao : debug
config: classpath:log4j2.yml # 指定log4j配置文件的位置