From 98bd032454c9dd01bcaea0f4c92abfa0b1847bac Mon Sep 17 00:00:00 2001 From: lindzh Date: Tue, 1 Aug 2017 20:12:46 +0800 Subject: [PATCH] =?UTF-8?q?Author:=20lindzh=20=20Author:?= =?UTF-8?q?=20=E9=B2=81=E8=88=AC=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #120 from lindzh/fix_client_logger. --- client/pom.xml | 10 +++ .../rocketmq/client/log/ClientLogger.java | 27 +++++-- .../main/resources/log4j2_rocketmq_client.xml | 48 +++++++++++++ .../rocketmq/client/log/ClientLogTest.java | 72 +++++++++++++++++++ example/pom.xml | 4 -- pom.xml | 7 +- 6 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 client/src/main/resources/log4j2_rocketmq_client.xml create mode 100644 client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java diff --git a/client/pom.xml b/client/pom.xml index 7b73f3fbf08..c7ab885b431 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 2da21241f4e..05d72b40ce0 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 @@ 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 @@ private static Logger createLogger(final String loggerName) { 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 @@ private static Logger createLogger(final String loggerName) { 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 @@ private static Logger createLogger(final String loggerName) { } 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 00000000000..651553e0b09 --- /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 00000000000..d7b619f51bf --- /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 947e5f2647b..ccebc4608dc 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 e5ef5e4c22b..1a03f92d654 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 +