Skip to content

Commit

Permalink
Author: lindzh <linsony0@163.com>
Browse files Browse the repository at this point in the history
Author: 鲁般 <dezhi.ldz@alibaba-inc.com>

Closes #120 from lindzh/fix_client_logger.
  • Loading branch information
lindzh authored and zhouxinyu committed Aug 1, 2017
1 parent 9bb6eae commit 98bd032
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 10 deletions.
10 changes: 10 additions & 0 deletions client/pom.xml
Expand Up @@ -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>
Expand Up @@ -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;
Expand All @@ -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"));

Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down
48 changes: 48 additions & 0 deletions 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>
@@ -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);
}
}
4 changes: 0 additions & 4 deletions example/pom.xml
Expand Up @@ -40,10 +40,6 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>

0 comments on commit 98bd032

Please sign in to comment.