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>