介绍
logback是java的日志开源组件,是log4j的作者开发的用来替代log4j的。
logback由三个部分组成,logback-core, logback-classic, logback-access。
其中logback-core是其他两个模块的基础。
logback-classic:它是log4j的一个改良版本,同时它完整实现了slf4j API,使我们可以在其他日志系统,如log4j和JDK14 Logging中进行转换。
logback-access:访问模块和Servlet容器集成,提供通过Http来访问日志的功能
整合
maven依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.1</version>
</dependency>
web配置
<!-- logback -->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:logback.xml</param-value>
</context-param>
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date %p [%thread] [%c{0}] => %m%n</pattern>
</encoder>
</appender>
<appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/videocontroller/service.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/smartphone/otherdays/service-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>104857600</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<MaxHistory>10</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n
</pattern>
<charset class="java.nio.charset.Charset">UTF-8</charset>
</encoder>
</appender>
<!--控制填输出日志级别-->
<root level="debug">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
<root level="INFO">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
<logger name="java.sql.Connection">
<level value="INFO"/>
<appender-ref ref="stdout"/>
</logger>
<logger name="java.sql.Statement">
<level value="INFO"/>
<appender-ref ref="stdout"/>
</logger>
<logger name="java.sql.PreparedStatement">
<level value="ERROR"/>
<appender-ref ref="stdout"/>
</logger>
</configuration>
来源:CSDN
作者:小小渔夫
链接:https://blog.csdn.net/cx243698/article/details/103454249