Repository: incubator-rocketmq Updated Branches: refs/heads/master 9bb6eae4b -> 98bd03245
Author: lindzh <linso...@163.com> Author: é²è¬ <dezhi....@alibaba-inc.com> Closes #120 from lindzh/fix_client_logger. Project: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/commit/98bd0324 Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/tree/98bd0324 Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/diff/98bd0324 Branch: refs/heads/master Commit: 98bd032454c9dd01bcaea0f4c92abfa0b1847bac Parents: 9bb6eae Author: lindzh <linso...@163.com> Authored: Tue Aug 1 20:12:46 2017 +0800 Committer: yukon <yu...@apache.org> Committed: Tue Aug 1 20:12:46 2017 +0800 ---------------------------------------------------------------------- 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(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/client/pom.xml ---------------------------------------------------------------------- diff --git a/client/pom.xml b/client/pom.xml index 7b73f3f..c7ab885 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -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> http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java ---------------------------------------------------------------------- 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 2da2124..05d72b4 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) { http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/client/src/main/resources/log4j2_rocketmq_client.xml ---------------------------------------------------------------------- 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 0000000..651553e --- /dev/null +++ b/client/src/main/resources/log4j2_rocketmq_client.xml @@ -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> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java ---------------------------------------------------------------------- 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 0000000..d7b619f --- /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); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/example/pom.xml ---------------------------------------------------------------------- diff --git a/example/pom.xml b/example/pom.xml index 947e5f2..ccebc46 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -41,10 +41,6 @@ <artifactId>logback-classic</artifactId> </dependency> <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-core</artifactId> - </dependency> - <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/98bd0324/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e5ef5e4..1a03f92 100644 --- a/pom.xml +++ b/pom.xml @@ -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>