1.logback-spring.xml
?xml version="1.0" encoding="UTF-8"?>
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--输出到文件-->
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/log_info.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/info/log-info-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- 时间滚动输出 SQL日志 -->
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/sql.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/sql/sql-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/error.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/error/error-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- 时间滚动输出 请求参数 日志 -->
<appender name="REQUEST_PARAM_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/request_param.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/request_param/request_param-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!--支付-->
<appender name="PAY_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/pay.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/pay/pay-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!--定时任务-->
<appender name="JOB_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGHOME}/job.log</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGHOME}/job/job-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!--支付-->
<logger name="PAY_LOG" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="PAY_FILE"/>
</logger>
<!--任务-->
<logger name="JOB_LOG" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="JOB_FILE"/>
</logger>
<!--错误-->
<logger name="ERROR_LOG" additivity="${ADDITIVITY}">
<level value="ERROR"/>
<appender-ref ref="ERROR_FILE"/>
</logger>
<!--请求参数-->
<logger name="INFO_LOG" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="INFO_FILE"/>
</logger>
<!--请求参数-->
<logger name="REQUEST_PARAM_LOG" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="REQUEST_PARAM_FILE"/>
</logger>
<!--sql日志输出设置-->
<logger name="cn.zhicall.web.dao" additivity="${ADDITIVITY}">
<level value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<!--第三方日志输出配置-->
<logger name="java.sql.Connection" additivity="${ADDITIVITY}">
<level value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<logger name="java.sql.Statement" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<logger name="java.sql.PreparedStatement" additivity="${ADDITIVITY}">
<level value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<logger name="com.alibaba.druid" additivity="${ADDITIVITY}">
<level value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<logger name="java.sql.ResultSet" additivity="${ADDITIVITY}">
<level value="INFO" />
<appender-ref ref="SQL_FILE" />
</logger>
<!-- spring的日志输入设置 -->
<logger name="org.springframework" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="INFO_FILE"/>
</logger>
<logger name="org.mybatis.spring.mapper" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="INFO_FILE"/>
</logger>
<!-- MyBatis日志输出设置 -->
<logger name="org.apache.ibatis" level="DEBUG" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<logger name="org.apache.ibatis.io.ResolverUtil" additivity="${ADDITIVITY}">
<level value="INFO"/>
<appender-ref ref="SQL_FILE"/>
</logger>
<root level="debug">
<appender-ref ref="CONSOLE" />
</root>
2.使用示例
package cn.test.web.controller.im;
import com.zhicall.framework.core.common.utils.log.LogProxy;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.concurrent.TimeUnit;
/**
- @author Herb
@create 2020-04-26 13:05
**/
@RestController
@RequestMapping("/test")
public class TestController {private Logger logger = LogProxy.getLogger("INFO_LOG");
private Logger logger2 = LogProxy.getLogger("ERROR_LOG");
@Autowired
private ImPlatformDoctorService imPlatformDoctorService;@Autowired
protected RedisTemplate redisTemplate;@GetMapping("/getuser")
public void getuser() {logger.info("进入getUser(INFO_LOG-INFO)"); logger.error("进入getUser(INFO_LOG-ERROR)"); logger2.error("进入getUser(ERROR_LOG-ERROR)"); imPlatformDoctorService.queryDoctorConfig(10407L, "1");
}