1. 程式人生 > >logback將日誌寫入不同資料夾裡

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

}

結果如下圖: