Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
*/
package org.apache.rocketmq.client.log;


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;

import java.lang.reflect.Method;
import java.net.URL;

public class ClientLogger {
public static final String CLIENT_LOG_ROOT = "rocketmq.client.logRoot";
Expand Down Expand Up @@ -54,57 +53,15 @@ 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);
}

} 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);
}
}
loadLoggerConfig(logConfigFilePath, log4JResourceFile, log4J2ResourceFile, logbackResourceFile);
} catch (Exception e) {
System.err.println(e);
}
}
return LoggerFactory.getLogger(LoggerName.CLIENT_LOGGER_NAME);
}


public static Logger getLog() {
if (log == null) {
log = createLogger(LoggerName.CLIENT_LOGGER_NAME);
Expand Down
112 changes: 112 additions & 0 deletions common/src/main/java/org/apache/rocketmq/common/utils/LogUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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
*/
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 configLog4j2(String logConfigFilePath,
String log4J2ResourceFile) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
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);
}
}


public static void loadLoggerConfig(String logConfigFilePath, String log4JResourceFile, String log4J2ResourceFile,
String logbackResourceFile) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
Class classType = iLoggerFactory.getClass();
if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
configLog4j(logConfigFilePath, log4JResourceFile);
} else if (classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
configLogback(logConfigFilePath, logbackResourceFile, iLoggerFactory);
}
else if (classType.getName().equals("org.apache.logging.slf4j.Log4jLoggerFactory")) {
configLog4j2(logConfigFilePath, log4J2ResourceFile);
}
}


}
76 changes: 76 additions & 0 deletions distribution/conf/log4j_tools.xml
Original file line number Diff line number Diff line change
@@ -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>

8 changes: 0 additions & 8 deletions tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +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;

Expand All @@ -28,6 +24,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;
Expand Down Expand Up @@ -72,6 +69,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 {
Expand All @@ -89,7 +87,7 @@ public static void main0(String[] args, RPCHook rpcHook) {
initCommand();

try {
initLogback();
initLog();
switch (args.length) {
case 0:
printHelp();
Expand Down Expand Up @@ -195,14 +193,23 @@ public static void initCommand() {
initCommand(new QueryConsumeQueueCommand());
}

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() {
Expand Down