zoukankan      html  css  js  c++  java
  • logback将日志写入不同文件夹里

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

    一、logback.xml文件配置如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
        <contextName>nana</contextName>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <Pattern>[%d{HH:mm:ss.SSS}] [%5level] [%thread] %logger{36} %msg%n</Pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <appender name="INFO_NANA" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../normalInfo.log</file>
    
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <!--过滤器,只打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.FixedWindowRollingPolicy">
                <fileNamePattern>normalInfo.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>
    
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>
        </appender>
    
    
        <appender name="ERROR_NANA" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../normalError.log</file>
    
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <!--过滤器,只打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.FixedWindowRollingPolicy">
                <fileNamePattern>normalError.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>
    
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>
        </appender>
    
        <!--不同业务逻辑的日志打印到不同文件-->
        <appender name="FIRST_NANA" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../firstInfo.log</file>
    
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <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.FixedWindowRollingPolicy">
                <fileNamePattern>firstInfo.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>
    
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>
        </appender>
    
        <appender name="SECOND_NANA" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>../secondInfo.log</file>
    
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
            <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.FixedWindowRollingPolicy">
                <fileNamePattern>secondInfo.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>3</maxIndex>
            </rollingPolicy>
    
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>
        </appender>
    
       <!-- 不同的业务逻辑日志打印到指定文件夹--> <logger name="first" level="info" additivity="false"> <appender-ref ref="FIRST_NANA"/> </logger> <logger name="second" level="info" additivity="false"> <appender-ref ref="SECOND_NANA"/> </logger> <!--info和error分开打印--> <root level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="INFO_NANA"/> <appender-ref ref="ERROR_NANA"/> </root> </configuration>

    二、工具类配置:

    创建与logback.xml的logger名相对应的对象:

    public enum LogFileName {
    
        // 与logback.xml的logger name 相同
        FIRST("first"),
        SECOND("second");
    
        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;
        }
    }

    指定logger日志存储路径,工具类:

    public class LoggerUtil {
        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());
        }
    }

    测试如下:

    @RestController
    public class UserController {
    
        Logger FIRST_LOG = LoggerUtil.logger(LogFileName.FIRST);
        Logger SECOND_LOG = LoggerUtil.logger(LogFileName.SECOND);
        
        @RequestMapping("/bb")
        public String bb() {
            FIRST_LOG.info("firsthaha");
            SECOND_LOG.info("secondhaha");
            return "bb";
        }
    
    }

    结果如下图:

  • 相关阅读:
    boostrap各种设备大小,以及不同设备的隐藏和显示
    thinkphp5 layout布局的理解和使用,模板继承等等
    thinkphp5的前台是如何实现的,以及后台栏目的作用
    JS工作流进度条显示,以及相关的思路和原理
    Lnmp服务器集群配置研究和实践
    关于require.js写的最通俗易懂的文章
    使用js把html导出word,单位换算,配置样式和分页,页边距,字体及大小,行距,缩进,@page等等
    使用phpword插件,导出word,并解决循环数据的问题
    php使用array_filter函数,并使用闭包,动态的过滤数组
    leetcode——437. 路径总和 III
  • 原文地址:https://www.cnblogs.com/nananana/p/10134671.html
Copyright © 2011-2022 走看看