1.logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <!--logback日志配置文件 注意事项: 1.更改对应LOGHOME(可变更为环境变量、指定Windows/Linux路径) 2.日志打印级别如需更改,直接更改logger里面的即可 3.sql打印路径为dao路径 使用说明: 1.在对应文件如:需要打印任务相关:protected final Logger job_logger = LogProxy.getLogger("JOB_LOG"); 2.getLogger("JOB_LOG")里面的name需和logger标签里保持一致 3.LogProxy引入路径:import com.zhicall.framework.core.common.utils.log.LogProxy; (Logger logger = org.slf4j.LoggerFactory.getLogger(loggerName)等效) 启动加载: 1.SpringBoot默认加载recources路径下名为:logback-spring的文件 2.手动在springboot配置文件(application.yml/application.properties)加载 3.按照当前智康在program arguments中指明(可以避免更改多个环境的问题) --> <configuration scan="true" scanPeriod="60 seconds" > <!-- 1.若是additivity设为false,则子Logger只会在自己的appender里输出,不会在root的logger的appender里输出 2.若是additivity设为true,则子Logger不止会在自己的appender里输出,还会在root的logger的appender里输出 --> <property name="ADDITIVITY" value="false"/> <property name="LEVEL" value="DEBUG"/> <property name="LOGHOME" value="D://hulog//"/> <property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %X{logChainId} %-5level %logger{50} - %msg%n"/> <!--输出到控制台--> <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> </configuration>
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"); } }