You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/08/17 07:43:51 UTC
[GitHub] diecui1202 closed pull request #2279: add log4j2 extension
diecui1202 closed pull request #2279: add log4j2 extension
URL: https://github.com/apache/incubator-dubbo/pull/2279
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/dubbo-common/pom.xml b/dubbo-common/pom.xml
index 72d43da6f5..22a13222fe 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -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>
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
index 7848224d95..ec98755000 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java
@@ -20,6 +20,7 @@
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 @@
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 @@
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());
+ }
}
}
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
new file mode 100644
index 0000000000..ed40b65766
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -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();
+ }
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
new file mode 100644
index 0000000000..76f29dd73e
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -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) {
+ }
+}
diff --git a/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter b/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter
index fb0b3d8f96..90d653c988 100644
--- a/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter
+++ b/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.logger.LoggerAdapter
@@ -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
\ No newline at end of file
+jdk=org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter
+log4j2=org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java
index 17e53f80b4..9234626b4e 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java
@@ -22,6 +22,8 @@
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 @@
{JclLoggerAdapter.class, JclLogger.class},
{JdkLoggerAdapter.class, JdkLogger.class},
{Log4jLoggerAdapter.class, Log4jLogger.class},
- {Slf4jLoggerAdapter.class, Slf4jLogger.class}
+ {Slf4jLoggerAdapter.class, Slf4jLogger.class},
+ {Log4j2LoggerAdapter.class, Log4j2Logger.class},
});
}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java
index ab1bae45cf..a8cb0c44e6 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java
@@ -35,7 +35,6 @@ public void testLocationAwareLogger() {
Slf4jLogger logger = new Slf4jLogger(locationAwareLogger);
logger.error("error");
- ;
logger.warn("warn");
logger.info("info");
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 29ca7a7c5e..81e4d45cdf 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -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>
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org