自定义Log4j配置文件和RocketMQ-Client.jar下log文件冲突问题解决
Posted Java硅谷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Log4j配置文件和RocketMQ-Client.jar下log文件冲突问题解决相关的知识,希望对你有一定的参考价值。
项目jar正常,我们项目log4j中已经配置了root日志级别level为info,但是启动项目后,自定义logger日志只能打印error级别。
启动服务时总是出现没有设置root logger的日志级别。
2018-05-09 16:24:34,309 main WARN No Root logger was configured,
creating default ERROR-level Root logger with Console appender
但是我们日志文件已经设置了info级别
<Root level="INFO">
<AppenderRef ref="ConsoleAppend"/>
</Root>
分析
经过debug发现,加载的日志文件不是我们配置的日志文件,而是rocketmq下的配置的日志文件。
打开该配置文件
<!DOCTYPE xml>
<Configuration status="warn" name="RocketmqClient">
<Appenders>
<Console name="STDOUT-APPENDER">
<PatternLayout pattern="%-5p %c{2} , %m%n"/>
</Console>
<RollingFile name="RocketmqClientAppender" fileName="${sys:client.logRoot}/rocketmq_client.log"
filePattern="${sys:client.logRoot}/rocketmq_client-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d{yyy-MM-dd HH\:mm\:ss,SSS} %p %c{1}(%L) - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="1 GB"/>
</Policies>
<DefaultRolloverStrategy max="${sys:client.logFileMaxIndex}"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="RocketmqClient" level="${sys:client.logLevel}" additivity="false">
<appender-ref ref="RocketmqClientAppender"/>
</logger>
<logger name="RocketmqCommon" level="${sys:client.logLevel}" additivity="false">
<appender-ref ref="RocketmqClientAppender"/>
</logger>
<logger name="RocketmqRemoting" level="${sys:client.logLevel}" additivity="false">
<appender-ref ref="RocketmqClientAppender"/>
</logger>
</Loggers>
</Configuration>
分析确实不存在root level的配置。
1、我们自定义的日志配置文件
2、目前我们使用的是rocketmq-4.2.0版本
3、分析mq日志配置源码ClientLogger
public class ClientLogger {
public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
public static final String CLIENT_LOG_MAXINDEX = "rocketmq.client.logFileMaxIndex";
public static final String CLIENT_LOG_LEVEL = "rocketmq.client.logLevel";
private static Logger log;
private static Logger createLogger(final String loggerName) {
//System.getProperty获取logConfigFilePath
String logConfigFilePath = System.getProperty("rocketmq.client.log.configFile", System.getenv("ROCKETMQ_CLIENT_LOG_CONFIGFILE"));
//是否加载rocketmq.client.log.loadconfig--默认为true
Boolean isloadconfig =
Boolean.parseBoolean(System.getProperty("rocketmq.client.log.loadconfig", "true"));
//log4J日志资源配置
final String log4JResourceFile =
System.getProperty("rocketmq.client.log4j.resource.fileName", "log4j_rocketmq_client.xml");
//logback日志资源配置
final String logbackResourceFile =
System.getProperty("rocketmq.client.logback.resource.fileName", "logback_rocketmq_client.xml");
//log4J2日志资源配置
final String log4J2ResourceFile =
System.getProperty("rocketmq.client.log4j2.resource.fileName", "log4j2_rocketmq_client.xml");
String clientLogRoot = System.getProperty(CLIENT_LOG_ROOT, System.getProperty("user.home") + "/logs/rocketmqlogs");
System.setProperty("client.logRoot", clientLogRoot);
String clientLogLevel = System.getProperty(CLIENT_LOG_LEVEL, "INFO");
System.setProperty("client.logLevel", clientLogLevel);
String clientLogMaxIndex = System.getProperty(CLIENT_LOG_MAXINDEX, "10");
System.setProperty("client.logFileMaxIndex", clientLogMaxIndex);
//允许加载配置
if (isloadconfig) {
try {
ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
Class classType = iLoggerFactory.getClass();
if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
Class<?> domconfigurator;
Object domconfiguratorobj;
domconfigurator = Class.forName("org.apache.log4j.xml.DOMConfigurator");
domconfiguratorobj = domconfigurator.newInstance();
if (null == logConfigFilePath) {
Method configure = domconfiguratorobj.getClass().getMethod("configure", URL.class);
URL url = org.apache.rocketmq.client.log.ClientLogger.class.getClassLoader().getResource(log4JResourceFile);
configure.invoke(domconfiguratorobj, url);
} else {
Method configure = domconfiguratorobj.getClass().getMethod("configure", String.class);
configure.invoke(domconfiguratorobj, logConfigFilePath);
}
} else if (classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
Class<?> joranConfigurator;
Class<?> context = Class.forName("ch.qos.logback.core.Context");
Object joranConfiguratoroObj;
joranConfigurator = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
joranConfiguratoroObj = joranConfigurator.newInstance();
Method setContext = joranConfiguratoroObj.getClass().getMethod("setContext", context);
setContext.invoke(joranConfiguratoroObj, iLoggerFactory);
if (null == logConfigFilePath) {
URL url = org.apache.rocketmq.client.log.ClientLogger.class.getClassLoader().getResource(logbackResourceFile);
Method doConfigure =
joranConfiguratoroObj.getClass().getMethod("doConfigure", URL.class);
doConfigure.invoke(joranConfiguratoroObj, url);
} else {
Method doConfigure =
joranConfiguratoroObj.getClass().getMethod("doConfigure", String.class);
doConfigure.invoke(joranConfiguratoroObj, logConfigFilePath);
}
} else if (classType.getName().equals("org.apache.logging.slf4j.Log4jLoggerFactory")) {
Class<?> joranConfigurator = Class.forName("org.apache.logging.log4j.core.config.Configurator");
Method initialize = joranConfigurator.getDeclaredMethod("initialize", String.class, String.class);
if (null == logConfigFilePath) {
initialize.invoke(joranConfigurator, "log4j2", log4J2ResourceFile);
} else {
initialize.invoke(joranConfigurator, "log4j2", logConfigFilePath);
}
}
} catch (Exception e) {
System.err.println(e);
}
}
return LoggerFactory.getLogger(LoggerName.CLIENT_LOGGER_NAME);
}
public static Logger getLog() {
if (log == null) {
log = createLogger(LoggerName.CLIENT_LOGGER_NAME);
return log;
} else {
return log;
}
}
public static void setLog(Logger log) {
org.apache.rocketmq.client.log.ClientLogger.log = log;
}
}
经过分析可知:我们可以通过 System.setProperty
来设置一些属性,屏蔽mq的日志配置,所以我们在mq的配置bean中设置:
RocketMQConfiguration(){
System.setProperty("rocketmq.client.log.loadconfig","false");
}
参考文章: https://www.cnblogs.com/devilfeng/p/6869166.html https://blog.csdn.net/wyfk2013/article/details/78218221
以上是关于自定义Log4j配置文件和RocketMQ-Client.jar下log文件冲突问题解决的主要内容,如果未能解决你的问题,请参考以下文章
自定义Log4j配置文件和RocketMQ-Client.jar下log文件冲突问题解决
Log4j 自定义配置为每个级别分隔日志 - Playframework 1.2.5
2019-07-03 log4j2 自定义ElasticSearch Appender
Java日志框架 -- LOG4J(Log4j入门案例日志级别Log4j组件(LoggersAppendersLayouts)配置文件内置日志记录自定义Logger)
Java日志框架 -- LOG4J(Log4j入门案例日志级别Log4j组件(LoggersAppendersLayouts)配置文件内置日志记录自定义Logger)