<?xml version="1.0" encoding="UTF-8"?> <configuration status="off"> <!--先定義所有的appender --> <appenders> <!--這個輸出控制檯的配置 --> <Console name="Console" target="SYSTEM_OUT"> <!--這個都知道是輸出日誌的格式 --> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" /> <!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch) --> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> </Console> <RollingFile name="RollingFile" fileName="/log/CFDMS.log" filePattern="/log/CFDMS.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> <Policies> <!-- 每天一個檔案 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 儲存365天的記錄 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <RollingFile name="RollingFileSuccess" fileName="/log/CFDMSSuccess.log" filePattern="/log/CFDMSSuccess.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <Filters> <!-- 顯示error級別之外的資訊 --> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <Policies> <!-- 每天一個檔案 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 儲存365天的記錄 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <RollingFile name="RollingFileError" fileName="/log/CFDMSError.log" filePattern="/log/CFDMSError.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <Filters> <!-- 只顯示error級別的資訊 --> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <Policies> <!-- 每天一個檔案 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 儲存365天的記錄 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <!-- 自定義一個log檔案在程式中直接使用 --> <RollingFile name="liuyxlog" fileName="/log/liuyx.log" append="true" filePattern="/log/liuyx$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <SizeBasedTriggeringPolicy size="4 MB"/> </RollingFile> </appenders> <!--然後定義logger,只有定義了logger並引入的appender,appender才會生效 --> <loggers> <!-- 3rdparty Loggers --> <logger name="org.springframework.core" level="warn"> </logger> <logger name="org.springframework.beans" level="warn"> </logger> <logger name="org.springframework.context" level="warn"> </logger> <logger name="org.springframework.web" level="warn"> </logger> <logger name="org.jboss.netty" level="warn"> </logger> <logger name="org.apache.http" level="warn"> </logger> <logger name="com.mchange.v2" level="warn"> </logger> <!-- 自定義一個log檔案在程式中直接使用 --> <logger name="liuyxlogger" additivity="TRUE" level="ALL"> <appender-ref ref="liuyxlog" level="ALL" /> </logger> <!--建立一個預設的root的logger --> <root level="info"> <appender-ref ref="RollingFile" /> <appender-ref ref="RollingFileSuccess" /> <appender-ref ref="RollingFileError" /> <appender-ref ref="Console" /> </root> </loggers> </configuration>
package com.inspur; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class WhereIsMyLogFile { private static Logger logger = LogManager.getLogger("liuyxlogger"); private static Logger logger2 = LogManager.getLogger(WhereIsMyLogFile.class); public static void main(String[] args) { logger.trace("hello liuyx log"); logger2.info("from logger2"); logger2.error("im an error"); } }
輸出結果
如果想輸出到tomcat根目錄下,可以在路徑前加上${sys:catalina.home}