具体配置如下:
1、 pom.xml文件中添加配置:
<!-- logback+slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<!-- logback扩展包,监听,用于解决与spring的结合 -->
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.4</version>
</dependency>
2、web.xml 配置:
<!-- 配置日志 logback-->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>/WEB-INF/config/logback.xml</param-value>
</context-param>
<!-- logback扩展,监听,用于解决与spring的结合 -->
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
3、logback.xml 配置 :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<!-- ch.qos.logback.core.rolling.RollingFileAppender 文件日志输出 -->
<appender name="mavenProjectlogs"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:/myProject/mavenProject.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:/myProject/mavenProject.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>5</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="springlogs"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>C:/myProject/mavenProject-spring.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>C:/myProject/mavenProject-spring.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>5</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!--精确设置每个包下面的日志 -->
<logger name="com" level="DEBUG">
<appender-ref ref="mavenProjectlogs" />
</logger>
<logger name="org.springframework" level="ERROR">
<appender-ref ref="springlogs" />
</logger>
<logger name="org.springframework.web" level="ERROR">
<appender-ref ref="springlogs" />
</logger>
<!-- Hibernate显示sql参数值 -->
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" />
<logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" />
<!-- <logger name="org.hibernate.SQL" level="DEBUG" />
<logger name="org.hibernate.type" level="TRACE" />
<logger name="org.hibernate.engine.QueryParameters" level="DEBUG" />
<logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> -->
<!-- 定义了ERROR和INFO级别的日志,分别在STDOUT/mavenProjectlogs输出 -->
<root level="ERROR">
<appender-ref ref="STDOUT" />
<!-- <appender-ref ref="mavenProjectlogs" /> -->
</root>
</configuration>