• logback不同业务的日志打印到不同文件


    logback不同业务的日志打印到不同文件

     版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mggwct/article/details/77718122

    在业务逻辑较为多的系统中,为了能快速的排查线上的问题和清楚的查询各个业务的日志信息,往往需要对不同业务线的日志进行分开记录: 
    比如现在系统中有对小金库用户发放奖励和白条用户进行发放奖励。

    logback.xml配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder charset="UTF-8">
                <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${catalina.base}/program/info.log</File>
            <append>true</append>
            <!--过滤器,只打INFO级别的日志-->
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base}/program/info.log.%d</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
    
            <encoder charset="UTF-8">
                <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${catalina.base}/program/error.log</File>
            <append>true</append>
            <!--过滤器,只打ERROR级别的日志-->
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base}/program/error.log.%d</fileNamePattern>
                <maxHistory>12</maxHistory>
            </rollingPolicy>
    
            <encoder charset="UTF-8">
                <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <!--不同业务逻辑的日志打印到不同文件-->
        <appender name="baitiaoUserAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${catalina.base}/program/bt.log</File>
            <append>true</append>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base}/program/bt.log.%d</fileNamePattern>
                <maxHistory>12</maxHistory>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <appender name="xjkUserAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <File>${catalina.base}/program/xjk.log</File>
            <append>true</append>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base}/program/xjk.log.%d</fileNamePattern>
                <maxHistory>12</maxHistory>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <!-- 不同的业务逻辑日志打印到指定文件夹-->
        <logger name="baitiaoUser" additivity="false" level="INFO">
            <appender-ref ref="baitiaoUserAppender"/>
        </logger>
        <logger name="xjkUser" additivity="false" level="INFO">
            <appender-ref ref="xjkUserAppender"/>
        </logger>
    
        <logger name="com.act.award" level="INFO"/>
        <logger name="com.act.award" level="DEBUG"/>
    
        <!--info和error分开打印-->
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="SYS_INFO"/>
            <appender-ref ref="SYS_ERROR"/>
        </root>
    
    </configuration>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    代码:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:/spring/application.xml")
    public class MainTest {
        Logger XJK_USER_LOG = LoggerUtils.Logger(LogFileName.XJK_USER);
        Logger BAITIAO_USER_LOG = LoggerUtils.Logger(LogFileName.BAITIAO_USER);
    
        @Test
        public void testGetBusinessAccount() throws Exception {
            XJK_USER_LOG.info("小金库用户进来了...");
            BAITIAO_USER_LOG.info("白条用户进来了...");
        }
    
    }
    
    public enum LogFileName {
    
        //配置到logback.xml中的logger name="vipUser"
        XJK_USER("xjkUser"),
        BAITIAO_USER("baitiaoUser");
    
        private String logFileName;
    
        LogFileName(String fileName) {
            this.logFileName = fileName;
        }
    
        public String getLogFileName() {
            return logFileName;
        }
    
        public void setLogFileName(String logFileName) {
            this.logFileName = logFileName;
        }
    
        public static LogFileName getAwardTypeEnum(String value) {
            LogFileName[] arr = values();
            for (LogFileName item : arr) {
                if (null != item && StringUtils.isNotBlank(item.logFileName)) {
                    return item;
                }
            }
            return null;
        }
    }
    
    public class LoggerUtils {
        public static <T> Logger Logger(Class<T> clazz) {
            return LoggerFactory.getLogger(clazz);
        }
    
        /**
         * 打印到指定的文件下
         *
         * @param desc 日志文件名称
         * @return
         */
        public static Logger Logger(LogFileName desc) {
            return LoggerFactory.getLogger(desc.getLogFileName());
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    日志文件形式:

    Paste_Image.png 
    bt.log内容: 
    INFO [main] MainTest.java:23 - 白条用户进来了… 
    xjk.log内容: 
    INFO [main] MainTest.java:22 - 小金库用户进来了…

  • 相关阅读:
    关于jquery尺寸的总结
    PhotoshopCC 如何使用动作文件ATN
    改变radio默认样式
    html中如何修改选中 用input做的搜索框 的边框颜色
    option触发事件两种方法总结
    用 CSS 隐藏页面元素的 5 种方法
    bootstrap模态框总结
    stop总结
    如何解决在chrome中自动完成表单后input出现黄色背景
    jquery实现显示和隐藏toggle()方法的使用
  • 原文地址:https://www.cnblogs.com/bigben0123/p/10856392.html
Copyright © 2020-2023  润新知