mirror of
https://github.com/apache/rocketmq.git
synced 2026-10-29 17:25:35 +08:00
Author: lindzh <linsony0@163.com>
Author: 鲁般 <dezhi.ldz@alibaba-inc.com> Closes #120 from lindzh/fix_client_logger.
This commit is contained in:
@@ -45,5 +45,15 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.apache.rocketmq.client.log;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.rocketmq.common.constant.LoggerName;
|
||||
import org.slf4j.ILoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
@@ -27,16 +28,15 @@ 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;
|
||||
|
||||
static {
|
||||
log = createLogger(LoggerName.CLIENT_LOGGER_NAME);
|
||||
}
|
||||
private static Class logClass = null;
|
||||
|
||||
private static Logger createLogger(final String loggerName) {
|
||||
String logConfigFilePath =
|
||||
System.getProperty("rocketmq.client.log.configFile",
|
||||
System.getenv("ROCKETMQ_CLIENT_LOG_CONFIGFILE"));
|
||||
System.getenv("ROCKETMQ_CLIENT_LOG_CONFIGFILE"));
|
||||
Boolean isloadconfig =
|
||||
Boolean.parseBoolean(System.getProperty("rocketmq.client.log.loadconfig", "true"));
|
||||
|
||||
@@ -46,6 +46,9 @@ public class ClientLogger {
|
||||
final String logbackResourceFile =
|
||||
System.getProperty("rocketmq.client.logback.resource.fileName", "logback_rocketmq_client.xml");
|
||||
|
||||
final String log4J2ResourceFile =
|
||||
System.getProperty("rocketmq.client.log4j2.resource.fileName", "log4j2_rocketmq_client.xml");
|
||||
|
||||
String clientLogRoot = System.getProperty(CLIENT_LOG_ROOT, "${user.home}/logs/rocketmqlogs");
|
||||
System.setProperty("client.logRoot", clientLogRoot);
|
||||
String clientLogLevel = System.getProperty(CLIENT_LOG_LEVEL, "INFO");
|
||||
@@ -90,7 +93,16 @@ public class ClientLogger {
|
||||
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);
|
||||
}
|
||||
}
|
||||
logClass = classType;
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
@@ -99,7 +111,12 @@ public class ClientLogger {
|
||||
}
|
||||
|
||||
public static Logger getLog() {
|
||||
return log;
|
||||
if (log == null) {
|
||||
log = createLogger(LoggerName.CLIENT_LOGGER_NAME);
|
||||
return log;
|
||||
} else {
|
||||
return log;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setLog(Logger log) {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<!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>
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.rocketmq.client.log;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Date;
|
||||
|
||||
public class ClientLogTest {
|
||||
|
||||
public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
|
||||
public static final String LOG_DIR;
|
||||
|
||||
static {
|
||||
LOG_DIR = System.getProperty(CLIENT_LOG_ROOT, "${user.home}/logs/rocketmqlogs");
|
||||
}
|
||||
|
||||
// FIXME: Workarond for concret implementation for slf4j, is there any better solution for all slf4j implementations in one class ? 2017/8/1
|
||||
@Test
|
||||
public void testLog4j2() throws IOException, NoSuchFieldException, IllegalAccessException {
|
||||
ClientLogger.getLog();
|
||||
long seek = 0;
|
||||
boolean result = false;
|
||||
File file = new File(LOG_DIR + File.separator + "rocketmq_client.log");
|
||||
if (file.exists()) {
|
||||
seek = file.length();
|
||||
}
|
||||
Field logClassField = ClientLogger.class.getDeclaredField("logClass");
|
||||
logClassField.setAccessible(true);
|
||||
Class logClass = (Class) logClassField.get(ClientLogger.class);
|
||||
Assert.assertEquals("org.apache.logging.slf4j.Log4jLoggerFactory", logClass.getName());
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ClientLogger.getLog().info("testcase testLog4j2 " + new Date());
|
||||
}
|
||||
|
||||
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
|
||||
randomAccessFile.seek(seek);
|
||||
String line = randomAccessFile.readLine();
|
||||
int idx = 1;
|
||||
while (line != null) {
|
||||
if (line.contains("testLog4j2")) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
line = randomAccessFile.readLine();
|
||||
idx++;
|
||||
if (idx > 20) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
randomAccessFile.close();
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
}
|
||||
@@ -40,10 +40,6 @@
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
|
||||
@@ -530,7 +530,7 @@
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.5</version>
|
||||
<version>1.7.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
@@ -592,6 +592,11 @@
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user