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

}

结果如下图:

 

posted @ 2018-12-17 23:21  娜娜娜娜小姐姐  阅读(3573)  评论(0编辑  收藏  举报