整合Logback,滚动记录+多文件

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
日志服务 SLS,月写入数据量 50GB 1个月
简介: 整合Logback,滚动记录+多文件

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");
    

    }

相关文章
|
2月前
|
Java
整合Logback,滚动记录+多文件
整合Logback,滚动记录+多文件
35 0
|
5月前
|
Java 应用服务中间件 HSF
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
|
8月前
|
SQL druid Java
三步实现maven工程集成logback日志框架(日志按天滚动生成文件)并附源码
三步实现maven工程集成logback日志框架(日志按天滚动生成文件)并附源码
308 0
|
Android开发 容器
Bug日志(一):RecyclerView只显示第一行
Bug日志(一):RecyclerView只显示第一行
|
消息中间件 Java 测试技术
logback异步输出日志详解
logback异步输出日志详解
5350 0
logback异步输出日志详解
|
XML 数据格式
日志文件logback.xml配置文件超简单(含中文注释)
这是我个人用到的logback.xml,我自己手写。
201 0
日志文件logback.xml配置文件超简单(含中文注释)
|
Java
logback过期日志文件自动删除
logback过期日志文件自动删除
1314 0
logback过期日志文件自动删除
|
XML Java 数据库连接
日志组件logback介绍及配置使用方法
转载于:http://sunney2010.iteye.com/blog/1404729 一、logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-cla
3347 0
IDEA 底下的indexing一直自动刷新刷新刷新刷新的解决方案
IDEA 底下的indexing一直自动刷新刷新刷新刷新的解决方案
1292 0
IDEA 底下的indexing一直自动刷新刷新刷新刷新的解决方案
|
Java
Logback日志文件位置关系详解
Logback日志文件位置关系详解
1105 0