diff --git a/client/pom.xml b/client/pom.xml
index 7b73f3fbf08554507b17568192afa56660b3b317..c7ab885b4315a51bad8e26545e08efb4958c3188 100644
--- a/client/pom.xml
+++ b/client/pom.xml
@@ -45,5 +45,15 @@
org.apache.commons
commons-lang3
+
+ org.apache.logging.log4j
+ log4j-core
+ test
+
+
+ org.apache.logging.log4j
+ log4j-slf4j-impl
+ test
+
diff --git a/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java b/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
index 2da21241f4e481a3f476118752004435b4e9eaaf..05d72b40ce08e43bd33d2903cab8c89f8a656514 100644
--- a/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
+++ b/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
@@ -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) {
diff --git a/client/src/main/resources/log4j2_rocketmq_client.xml b/client/src/main/resources/log4j2_rocketmq_client.xml
new file mode 100644
index 0000000000000000000000000000000000000000..651553e0b09336e4e36e5b2e0be162aee4832d01
--- /dev/null
+++ b/client/src/main/resources/log4j2_rocketmq_client.xml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java b/client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..d7b619f51bf2c79a7474f871599c84eb777c3958
--- /dev/null
+++ b/client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java
@@ -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);
+ }
+}
diff --git a/example/pom.xml b/example/pom.xml
index 947e5f2647bb35f788c7c6851cc9524de0360799..ccebc4608dcef3cf5edf9e29d60543dd748f7b78 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -40,10 +40,6 @@
ch.qos.logback
logback-classic
-
- ch.qos.logback
- logback-core
-
org.javassist
javassist
diff --git a/pom.xml b/pom.xml
index e5ef5e4c22bf5ffc585f0cb1a2bae454250ec3a6..1a03f92d6542be105a88e6204f1185bd4f6090b5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -530,7 +530,7 @@
org.slf4j
slf4j-api
- 1.7.5
+ 1.7.7
ch.qos.logback
@@ -592,6 +592,11 @@
log4j-core
2.7
+
+ org.apache.logging.log4j
+ log4j-slf4j-impl
+ 2.7
+