lo4j2配置示例

Posted 走路带_风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lo4j2配置示例相关的知识,希望对你有一定的参考价值。

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

 private Class clazz;
 private Logger logger;
 
public AppLog(Class clazz)
    this.clazz = clazz;
    this.logger = LogManager.getLogger(this.clazz);

 

lo4j2.properties

name = PropertiesConfig

filter.threshold.type = ThresholdFilter
filter.threshold.level = INFO

#root
rootLogger=info,console,mian_log,custom_log
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.level =INFO

#console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout     //通过ThreadContext.put("hostAddr", "value")添加进去
appender.console.layout.pattern = %m%n%dyyyy/MM/dd HH:mm:ss,SSS %-5p %XhostAddr [%XsessionId] %m [%c]%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = INFO
logger.console.name = mujinetstore.batch

#mian_log  ->  aaa.bbb.ccc
appender.mian_log.type = RollingFile
appender.mian_log.name = mian_log
appender.mian_log.fileName = /xxx/aaaa/mian_log.log  //log路径
appender.mian_log.filePattern = /xxx/aaaa/mian_log-%dMM-dd-yyyy.log.gz
appender.mian_log.layout.type = PatternLayout
appender.mian_log.layout.pattern = %dyyyy/MM/dd HH:mm:ss,SSS %-5p %XhostAddr [%XsessionId] %m [%c]%n
appender.mian_log.policies.type = Policies

logger.mian_log.name = aaa.bbb.ccc//指定这个包下面的所有log
logger.mian_log.level = INFO
logger.mian_log.additivity = true
logger.mian_log.appenderRef.rolling.ref = mian_log


#custom_log -> customLog.log
appender.custom_log.type = RollingFile
appender.custom_log.name = custom_log
appender.custom_log.fileName = /xxx/aaaa/customLog.log
appender.custom_log.filePattern = /xxx/aaaa/customLog-%dMM-dd-yyyy.log.gz
appender.custom_log.layout.type = PatternLayout
appender.custom_log.layout.pattern = "%dyyyy/MM/dd HH:mm:ss",%m%n
appender.custom_log.policies.type = Policies

logger.back_trace_log.name = customLog  //指定"customLog"
logger.back_trace_log.level = INFO
logger.back_trace_log.additivity = true
logger.back_trace_log.appenderRef.rolling.ref = custom_log

//customLog
static final String name = "customLog";
public static Logger logger = LogManager.getLogger(name);

 

ThreadContext.put("hostAddr", "value");

MDC和ThreadContext
MDC使用Map机制来存储信息,信息以key/value对的形式存储在Map中。
使用Servlet Filter在接收到请求时,生成UUID填充MDC。在log4j的配置中就可以使用%Xkey打印MDC中的内容,从而识别出同一次请求中的log

在log4j 2.0 中,使用ThreadContext代替了MDC
 

以上是关于lo4j2配置示例的主要内容,如果未能解决你的问题,请参考以下文章

也爆雷了。。。

也爆雷了。。。

log4j2 中基于时间的触发策略

log4j2 - Syslog 附加程序和 PatternLayout

华为运营商级路由器配置示例 | 合集

华为运营商级路由器配置示例 | 配置动态VPWS接入VPLS示例