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

    结果如下图:

  • 相关阅读:
    Asp.net分页控件AspNetPager的使用(DataList和Reapter结合Linq的编辑与删除)
    Zend Framework使用Extjs进行数据库交互和分页
    课程设计之(struts2+Hibernate)航空订票系统
    课程设计之(ZendFrameWrok)构建的航空订票系统网站
    Ubuntu10.10 Zend FrameWork配置及helloworld显示
    ExtJs、Struts2、Hibernate3.2登录页面的简单实现
    Asp.net的FileUpload控件的文件上传与Extjs文件上传的简单Demo
    学习linux的一些网络资源
    100条超搞笑的“雷人”QQ/MSN 签名
    超难的75道逻辑思维题
  • 原文地址:https://www.cnblogs.com/nananana/p/10134671.html
Copyright © 2011-2022 走看看