zhouxinyu closed pull request #85: [ROCKETMQ-158]Remove logback dependency for
rocketmq-tools
URL: https://github.com/apache/rocketmq/pull/85
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 2da21241..08d96096 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
@@ -16,12 +16,11 @@
*/
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;
import org.slf4j.LoggerFactory;
+import static org.apache.rocketmq.common.utils.LogUtils.loadLoggerConfig;
public class ClientLogger {
public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
@@ -55,42 +54,7 @@ private static Logger createLogger(final String loggerName) {
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 =
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 =
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);
- }
-
- }
+ loadLoggerConfig(logConfigFilePath, log4JResourceFile,
logbackResourceFile);
} catch (Exception e) {
System.err.println(e);
}
@@ -98,6 +62,7 @@ private static Logger createLogger(final String loggerName) {
return LoggerFactory.getLogger(LoggerName.CLIENT_LOGGER_NAME);
}
+
public static Logger getLog() {
return log;
}
diff --git
a/common/src/main/java/org/apache/rocketmq/common/utils/LogUtils.java
b/common/src/main/java/org/apache/rocketmq/common/utils/LogUtils.java
new file mode 100644
index 00000000..f14fd35d
--- /dev/null
+++ b/common/src/main/java/org/apache/rocketmq/common/utils/LogUtils.java
@@ -0,0 +1,97 @@
+/*
+ * 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.common.utils;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.LoggerFactory;
+
+public class LogUtils {
+
+ /**
+ * config logback dynamically from provided file path, otherwise try to
config from resource file
+ * @param logConfigFilePath specified logback configuration file path
+ * @param logbackResourceFile specified logback configuration resource
file, which will only be respected when logConfigFilePath is null
+ * @param iLoggerFactory
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ * @throws NoSuchMethodException
+ * @throws InvocationTargetException
+ */
+ public static void configLogback(String logConfigFilePath, String
logbackResourceFile, ILoggerFactory iLoggerFactory) throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
+ 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) {
+ if (logbackResourceFile != null) {
+ URL url =
LogUtils.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);
+ }
+ }
+
+ /**
+ * config log4j dynamically from provided file path, otherwise try to
config from resource file. Notice that the log4j configuration file should be
in xml format
+ * @param logConfigFilePath specified log4j configuration file path
+ * @param log4JResourceFile specified log4j configuration resource file,
which will only be respected when logConfigFilePath is null
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ * @throws NoSuchMethodException
+ * @throws InvocationTargetException
+ */
+ @Deprecated
+ public static void configLog4j(String logConfigFilePath, String
log4JResourceFile) throws ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException {
+ Class<?> domconfigurator;
+ Object domconfiguratorobj;
+ domconfigurator =
Class.forName("org.apache.log4j.xml.DOMConfigurator");
+ domconfiguratorobj = domconfigurator.newInstance();
+ if (null == logConfigFilePath) {
+ if (log4JResourceFile != null) {
+ Method configure =
domconfiguratorobj.getClass().getMethod("configure", URL.class);
+ URL url =
LogUtils.class.getClassLoader().getResource(log4JResourceFile);
+ configure.invoke(domconfiguratorobj, url);
+ }
+ } else {
+ Method configure =
domconfiguratorobj.getClass().getMethod("configure", String.class);
+ configure.invoke(domconfiguratorobj, logConfigFilePath);
+ }
+ }
+
+
+ public static void loadLoggerConfig(String logConfigFilePath, String
log4JResourceFile,
+ String logbackResourceFile) throws ClassNotFoundException,
InstantiationException, IllegalAccessException, NoSuchMethodException,
InvocationTargetException {
+ ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
+ Class classType = iLoggerFactory.getClass();
+ if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
+ LogUtils.configLog4j(logConfigFilePath, log4JResourceFile);
+ } else if
(classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
+ LogUtils.configLogback(logConfigFilePath, logbackResourceFile,
iLoggerFactory);
+ }
+ }
+}
diff --git a/distribution/conf/log4j_tools.xml
b/distribution/conf/log4j_tools.xml
new file mode 100644
index 00000000..c21adece
--- /dev/null
+++ b/distribution/conf/log4j_tools.xml
@@ -0,0 +1,76 @@
+<?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 log4j:configuration SYSTEM "log4j.dtd">
+
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+ <appender name="STDOUT-APPENDER" class="org.apache.log4j.ConsoleAppender">
+ <param name="encoding" value="UTF-8"/>
+ <param name="target" value="System.out"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%-5p %c{2} , %m%n"/>
+ </layout>
+ </appender>
+
+
+ <appender name="DefaultAppender"
class="org.apache.log4j.RollingFileAppender">
+ <param name="file"
value="${user.home}/logs/rocketmqlogs/otherdays/tools_default.%i.log"/>
+ <param name="encoding" value="UTF-8"/>
+ <param name="append" value="true"/>
+ <param name="maxFileSize" value="1073741824"/>
+ <param name="maxBackupIndex" value="5"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d{yyy-MM-dd
HH\:mm\:ss,SSS} %p %c{1}(%L) - %m%n"/>
+ </layout>
+ </appender>
+
+ <appender name="RocketmqToolsAppender"
class="org.apache.log4j.RollingFileAppender">
+ <param name="file" value="${user.home}/logs/rocketmqlogs/tools.log"/>
+ <param name="encoding" value="UTF-8"/>
+ <param name="append" value="true"/>
+ <param name="maxFileSize" value="1073741824"/>
+ <param name="maxBackupIndex" value="5"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d{yyy-MM-dd
HH\:mm\:ss,SSS} %p %c{1}(%L) - %m%n"/>
+ </layout>
+ </appender>
+
+ <logger name="RocketmqClient" additivity="false">
+ <level value="${client.logLevel}"/>
+ <appender-ref ref="RocketmqToolsAppender"/>
+ </logger>
+
+ <logger name="RocketmqCommon" additivity="false">
+ <level value="${client.logLevel}"/>
+ <appender-ref ref="RocketmqToolsAppender"/>
+ </logger>
+
+ <logger name="RocketmqRemoting" additivity="false">
+ <level value="${client.logLevel}"/>
+ <appender-ref ref="RocketmqToolsAppender"/>
+ </logger>
+
+ <logger name="RocketmqStore" additivity="false">
+ <level value="INFO"/>
+ <appender-ref ref="RocketmqToolsAppender"/>
+ </logger>
+
+</log4j:configuration>
+
diff --git a/tools/pom.xml b/tools/pom.xml
index d10194c1..42e4c037 100644
--- a/tools/pom.xml
+++ b/tools/pom.xml
@@ -48,14 +48,6 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
diff --git
a/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java
b/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java
index 9bd37e84..2c9cb0f1 100644
--- a/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java
+++ b/tools/src/main/java/org/apache/rocketmq/tools/command/MQAdminStartup.java
@@ -16,9 +16,6 @@
*/
package org.apache.rocketmq.tools.command;
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.spi.JoranException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.cli.CommandLine;
@@ -26,6 +23,7 @@
import org.apache.commons.cli.PosixParser;
import org.apache.rocketmq.common.MQVersion;
import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.utils.LogUtils;
import org.apache.rocketmq.remoting.RPCHook;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.srvutil.ServerUtil;
@@ -69,6 +67,7 @@
import org.apache.rocketmq.tools.command.topic.UpdateOrderConfCommand;
import org.apache.rocketmq.tools.command.topic.UpdateTopicPermSubCommand;
import org.apache.rocketmq.tools.command.topic.UpdateTopicSubCommand;
+import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
public class MQAdminStartup {
@@ -86,7 +85,7 @@ public static void main0(String[] args, RPCHook rpcHook) {
initCommand();
try {
- initLogback();
+ initLog();
switch (args.length) {
case 0:
printHelp();
@@ -191,14 +190,23 @@ public static void initCommand() {
initCommand(new GetBrokerConfigCommand());
}
- private static void initLogback() throws JoranException {
- String rocketmqHome =
System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY,
System.getenv(MixAll.ROCKETMQ_HOME_ENV));
-
- LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
- JoranConfigurator configurator = new JoranConfigurator();
- configurator.setContext(lc);
- lc.reset();
- configurator.doConfigure(rocketmqHome + "/conf/logback_tools.xml");
+ private static void initLog() {
+ try {
+ String rocketmqHome =
System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY,
System.getenv(MixAll.ROCKETMQ_HOME_ENV));
+ ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
+ Class classType = iLoggerFactory.getClass();
+ if
(classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
+ final String logfjConfigPath = rocketmqHome +
"/distribution/conf/log4j_tools.xml";
+ LogUtils.configLog4j(logfjConfigPath,null);
+ } else if
(classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
+ final String logbackConfigPath = rocketmqHome +
"/distribution/conf/logback_tools.xml";
+ LogUtils.configLogback(logbackConfigPath,null,iLoggerFactory);
+ } else {
+ System.out.printf("No Logback or Log4j implementations are
detected, ignore dynamical logging config.%n");
+ }
+ } catch (Exception e) {
+ System.err.println(e);
+ }
}
private static void printHelp() {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services