add log4j2 extension (#2279)

* add log4j2 for dubbo logging, #1713
* remove unused code, add provided scope to log4j2
This commit is contained in:
玩玩跑跑 2018-08-17 15:43:50 +08:00 committed by Jerrick Zhu
parent b2ce7b2c10
commit af2a538d3d
8 changed files with 273 additions and 5 deletions

View File

@ -43,6 +43,16 @@
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.jcl.JclLoggerAdapter;
import org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter;
import org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter;
import org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter;
import org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter;
import org.apache.dubbo.common.logger.support.FailsafeLogger;
@ -47,6 +48,8 @@ public class LoggerFactory {
setLoggerAdapter(new Log4jLoggerAdapter());
} else if ("jdk".equals(logger)) {
setLoggerAdapter(new JdkLoggerAdapter());
} else if ("log4j2".equals(logger)) {
setLoggerAdapter(new Log4j2LoggerAdapter());
} else {
try {
setLoggerAdapter(new Log4jLoggerAdapter());
@ -55,9 +58,13 @@ public class LoggerFactory {
setLoggerAdapter(new Slf4jLoggerAdapter());
} catch (Throwable e2) {
try {
setLoggerAdapter(new JclLoggerAdapter());
setLoggerAdapter(new Log4j2LoggerAdapter());
} catch (Throwable e3) {
setLoggerAdapter(new JdkLoggerAdapter());
try {
setLoggerAdapter(new JclLoggerAdapter());
} catch (Throwable e4) {
setLoggerAdapter(new JdkLoggerAdapter());
}
}
}
}

View File

@ -0,0 +1,128 @@
/*
* 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.dubbo.common.logger.log4j2;
import org.apache.dubbo.common.logger.Logger;
public class Log4j2Logger implements Logger {
private final org.apache.logging.log4j.Logger logger;
public Log4j2Logger(org.apache.logging.log4j.Logger logger) {
this.logger = logger;
}
@Override
public void trace(String msg) {
logger.trace(msg);
}
@Override
public void trace(Throwable e) {
logger.trace(e == null ? null : e.getMessage(), e);
}
@Override
public void trace(String msg, Throwable e) {
logger.trace(msg, e);
}
@Override
public void debug(String msg) {
logger.debug(msg);
}
@Override
public void debug(Throwable e) {
logger.debug(e == null ? null : e.getMessage(), e);
}
@Override
public void debug(String msg, Throwable e) {
logger.debug(msg, e);
}
@Override
public void info(String msg) {
logger.info(msg);
}
@Override
public void info(Throwable e) {
logger.info(e == null ? null : e.getMessage(), e);
}
@Override
public void info(String msg, Throwable e) {
logger.info(msg, e);
}
@Override
public void warn(String msg) {
logger.warn(msg);
}
@Override
public void warn(Throwable e) {
logger.warn(e == null ? null : e.getMessage(), e);
}
@Override
public void warn(String msg, Throwable e) {
logger.warn(msg, e);
}
@Override
public void error(String msg) {
logger.error(msg);
}
@Override
public void error(Throwable e) {
logger.error(e == null ? null : e.getMessage(), e);
}
@Override
public void error(String msg, Throwable e) {
logger.error(msg, e);
}
@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
@Override
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
@Override
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
@Override
public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}
@Override
public boolean isErrorEnabled() {
return logger.isErrorEnabled();
}
}

View File

@ -0,0 +1,107 @@
/*
* 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.dubbo.common.logger.log4j2;
import org.apache.dubbo.common.logger.Level;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerAdapter;
import org.apache.logging.log4j.LogManager;
import java.io.File;
public class Log4j2LoggerAdapter implements LoggerAdapter {
private Level level;
public Log4j2LoggerAdapter() {
}
private static org.apache.logging.log4j.Level toLog4j2Level(Level level) {
if (level == Level.ALL) {
return org.apache.logging.log4j.Level.ALL;
}
if (level == Level.TRACE) {
return org.apache.logging.log4j.Level.TRACE;
}
if (level == Level.DEBUG) {
return org.apache.logging.log4j.Level.DEBUG;
}
if (level == Level.INFO) {
return org.apache.logging.log4j.Level.INFO;
}
if (level == Level.WARN) {
return org.apache.logging.log4j.Level.WARN;
}
if (level == Level.ERROR) {
return org.apache.logging.log4j.Level.ERROR;
}
return org.apache.logging.log4j.Level.OFF;
}
private static Level fromLog4j2Level(org.apache.logging.log4j.Level level) {
if (level == org.apache.logging.log4j.Level.ALL) {
return Level.ALL;
}
if (level == org.apache.logging.log4j.Level.TRACE) {
return Level.TRACE;
}
if (level == org.apache.logging.log4j.Level.DEBUG) {
return Level.DEBUG;
}
if (level == org.apache.logging.log4j.Level.INFO) {
return Level.INFO;
}
if (level == org.apache.logging.log4j.Level.WARN) {
return Level.WARN;
}
if (level == org.apache.logging.log4j.Level.ERROR) {
return Level.ERROR;
}
return Level.OFF;
}
@Override
public Logger getLogger(Class<?> key) {
return new Log4j2Logger(LogManager.getLogger(key));
}
@Override
public Logger getLogger(String key) {
return new Log4j2Logger(LogManager.getLogger(key));
}
@Override
public Level getLevel() {
return level;
}
@Override
public void setLevel(Level level) {
this.level = level;
}
@Override
public File getFile() {
return null;
}
@Override
public void setFile(File file) {
}
}

View File

@ -1,4 +1,5 @@
slf4j=org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter
jcl=org.apache.dubbo.common.logger.jcl.JclLoggerAdapter
log4j=org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter
jdk=org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter
jdk=org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter
log4j2=org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter

View File

@ -22,6 +22,8 @@ import org.apache.dubbo.common.logger.jdk.JdkLogger;
import org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter;
import org.apache.dubbo.common.logger.log4j.Log4jLogger;
import org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter;
import org.apache.dubbo.common.logger.log4j2.Log4j2Logger;
import org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter;
import org.apache.dubbo.common.logger.slf4j.Slf4jLogger;
import org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter;
import org.junit.Test;
@ -42,7 +44,8 @@ public class LoggerAdapterTest {
{JclLoggerAdapter.class, JclLogger.class},
{JdkLoggerAdapter.class, JdkLogger.class},
{Log4jLoggerAdapter.class, Log4jLogger.class},
{Slf4jLoggerAdapter.class, Slf4jLogger.class}
{Slf4jLoggerAdapter.class, Slf4jLogger.class},
{Log4j2LoggerAdapter.class, Log4j2Logger.class},
});
}

View File

@ -35,7 +35,6 @@ public class Slf4jLoggerTest {
Slf4jLogger logger = new Slf4jLogger(locationAwareLogger);
logger.error("error");
;
logger.warn("warn");
logger.info("info");

View File

@ -106,6 +106,8 @@
<jcl_version>1.2</jcl_version>
<log4j_version>1.2.16</log4j_version>
<logback_version>1.2.2</logback_version>
<log4j2_version>2.11.1</log4j2_version>
<embedded_redis_version>0.6</embedded_redis_version>
<jaxb_version>2.2.7</jaxb_version>
@ -331,6 +333,17 @@
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2_version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2_version}</version>
</dependency>
<!-- for dubbo-rpc-webservice -->
<dependency>