log4j.xml和log4j2.xml配置應用日誌與業務日誌分離
業務場景:為了便於定位生產環境的問題定位和業務日誌資料的分析,需要將業務日誌和應用日誌分離
log4j.xml配置如下:
其中:dailyRolling是父級,bizDailyRolling是子級,要實現子級日誌不在父級中出現,關鍵在於log4j.additivity.bizDailyRolling=false
log4j.rootLogger=DEBUG,dailyRolling
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%F:%L] : %m%n
##應用日誌輸出配置
log4j.logger.dailyRolling=DEBUG,dailyRolling
log4j.additivity.dailyRolling=false
log4j.appender.dailyRolling=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRolling.File=../logs/boss_web.log
# Pattern to output: date priority [category] - message
log4j.appender.dailyRolling.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.dailyRolling.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyRolling.layout.ConversionPattern=%d [%t] %-5p [%F:%L] : %m%n
log4j.appender.dailyRolling.BufferedIO=false
log4j.appender.dailyRolling.BufferSize=8192
##業務日誌單獨輸出配置
##子級bizDailyRolling的日誌不在父級dailyRolling出現重複輸出
log4j.logger.bizDailyRolling=WARN,bizDailyRolling
log4j.additivity.bizDailyRolling=false
log4j.appender.bizDailyRolling=org.apache.log4j.DailyRollingFileAppender
#日誌輸出等級分離:只輸出該等級以上的日誌
log4j.appender.bizDailyRolling.Threshold=WARN
log4j.appender.bizDailyRolling.File=../logs/request/app_request.log
# Pattern to output: date priority [category] - message
log4j.appender.bizDailyRolling.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.bizDailyRolling.layout=org.apache.log4j.PatternLayout
log4j.appender.bizDailyRolling.layout.ConversionPattern=%d [%t] %-5p [%F:%L] : %m%n
log4j.appender.bizDailyRolling.Encoding=UTF-8
Java類中使用:
// 父級應用日誌
private static final Logger LOGGER = LoggerFactory.getLogger(AldNotifyController.class);
// 子級業務日誌
private static final Logger signDailyRolling = LoggerFactory.getLogger("bizDailyRolling");
LOGGER.info("向平臺業務資訊同步請求報文:{}",reqXml);
signDailyRolling.warn("signDailyRolling向平臺業務資訊同步請求報文:{}",reqXml);
log4j2.xml配置如下:
<!--父級應用日誌 RollingFile -->
<RollingRandomAccessFile name="RollingFile"
fileName="${LOG_HOME}/${APPLICATION_NAME}.log"
filePattern="${LOG_HOME}/${APPLICATION_NAME}_%d{yyyy-MM-dd-HH}.log">
<PatternLayout
pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%t] %F:%L - %msg%n" />
<Policies>
<!--<TimeBasedTriggeringPolicy modulate="true" interval="24"/> -->
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingRandomAccessFile>
<!-- 子級業務日誌 GlobalBasicLogFile -->
<RollingFile name="GlobalBasicLogFile" fileName="${LOG_HOME}/global/basic.log"
filePattern="${LOG_HOME}/global/basic_%d{yyyy-MM-dd}.log">
<PatternLayout>
<Charset>UTF-8</Charset>
<Pattern>${LOG_PATTERN_BUSINESS}</Pattern>
</PatternLayout>
<Policies>
<!-- <SizeBasedTriggeringPolicy size="10MB"/> -->
<!-- 每天建立一個日誌檔案 -->
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG_HOME}" maxDepth="2">
<IfFileName glob="basic_*.log"/>
<IfLastModified age="30d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<!--Logger節點用來單獨指定日誌的形式,比如要為指定包下的class指定不同的日誌級別等。 -->
<!--然後定義loggers,只有定義了logger並引入的appender,appender才會生效 -->
<Loggers>
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.apache.zookeeper" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<logger name="com.aspire" level="INFO"></logger>
<!-- 子級業務日誌 -->
<logger name="GlobalBasicLogFile" level="DEBUG" additivity="false">
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="GlobalBasicLogFile"/>
</logger>
<root level="debug">
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</Loggers>