1、引入jar包
<slf4j.version>1.7.30</slf4j.version> <logback.version>1.2.3</logback.version> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency>
2、引入logback.xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<property name="log_home" value="./" />
<!-- 服务名称,自行修改成各自的服务名称 -->
<property name="app.name" value="test-server" />
<!-- sql -->
<appender name="FILE-JDBC" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log_home}/${app.name}_jdbc.log</File>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-5thread] [%X{request.url}] %logger{20} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>warn</level>
</filter>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log_home}/${app.name}_jdbc_%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- 控制台输出日志,方便本地测试使用 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 默认日志输出文件,每天生成一个,保留最近30天的日志 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_home}/logs/${app.name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log_home}/logs/${app.name}-.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 记录数据源日志 -->
<appender name="log4jdbc" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_home}/logs/${app.name}-log4jdbc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log_home}/logs/${app.name}-log4jdbc-%d{yyyyMMdd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%-5level] [%-5thread] [%X{request.url}] %logger{20} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.huazhu.workermgmtservice" additivity="false" level="info">
<appender-ref ref="file" />
<!-- 控制台输出,本地调试时可打开,正式环境注释掉 -->
<appender-ref ref="stdout" />
</logger>
<!--打印SQL-->
<logger name="java.sql" level="DEBUG">
<appender-ref ref="log4jdbc" />
</logger>
<logger name="java.sql.Connection" level="DEBUG" >
<appender-ref ref="log4jdbc" />
</logger>
<logger name="java.sql.Statement" level="DEBUG" >
<appender-ref ref="log4jdbc" />
</logger>
<logger name="java.sql.PreparedStatement" level="DEBUG" >
<appender-ref ref="log4jdbc" />
</logger>
<!-- 记录sql执行的时间,可以分析耗时的sql语句 -->
<logger name="jdbc.sqltiming" additivity="false">
<appender-ref ref="FILE-JDBC" />
<!--debug 控制输出方法名,appender中filter过滤debug级别以下的日志 -->
<level value="debug" />
</logger>
<root level="info">
<!-- 控制台输出,本地调试时可打开,正式环境注释掉 -->
<appender-ref ref="stdout" />
<appender-ref ref="log4jdbc" />
</root>
</configuration>
3、使用
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private final static Logger logger = LoggerFactory.getLogger(YfLaborInfoServiceImpl.class); logger.info("selectListByCardProjectId info yfLaborInfoDto is {},token is {}", yfLaborInfoDto, token);
4、为何使用slf4j和logback