slf4j-api整合maven 工程日志配置文件

springmvc项目

pom.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

log.properties:

log.level=debug
log.path=../logs

配置文件logback.xml:

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<property resource="log.properties"/>

<!-- 控制台输出 -->
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger \(%file:%line\) - %X{trackInfo} %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
</appender>


<!-- 按照每天生成日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${log.path}/log-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern> -->

<pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger - %X{trackInfo} %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!--日志文件最大的大小 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>64MB</MaxFileSize>
</triggeringPolicy>
</appender>

<logger name="org.springframework" level="WARN" />
<logger name="org.mybatis" level="WARN"/>

<!-- 日志输出级别 -->
<root level="${log.level}">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>

复制代码

SpringBoot项目:

pom.xml:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>

application.properties:

log: 
  path: ../logs
  level: info

logback-spring.xml:

复制代码

<!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
<configuration scan="true" scanPeriod="10 seconds">
<springProperty scope="context" name="logLevel" source="log.level"/>
<springProperty scope="context" name="logPath" source="log.path"/>
<!-- Simple file output -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger - %X{trackInfo} %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily 配置日志所生成的目录以及生成文件名的规则 -->
<fileNamePattern>${logPath}/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 64 MB -->
<maxFileSize>64 MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>


<!-- Console output -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{yy-MM-dd HH:mm:ss.sss} [%thread] %-5level %logger \(%file:%line\) - %X{trackInfo} %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
</appender>


<logger name="jndi" level="WARN" />
<logger name="org.eclipse" level="WARN" />
<logger name="org.springframework" level="WARN" />
<logger name="org.mongodb" level="WARN" />
<logger name="org.mybatis" level="WARN"/>

<root level="${logLevel}">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>

<!-- For loggers in the these namespaces, log at all levels. -->
<logger name="pedestal" level="ALL" />
<logger name="hammock-cafe" level="ALL" />
<logger name="user" level="ALL" />
</configuration>

复制代码

 

 
 
posted @ 2021-11-09 15:22  牧之丨  阅读(694)  评论(0编辑  收藏  举报