dubbo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] diecui1202 closed pull request #2281: [Dubbo-1713] add the log4j2 support #1713 team2
Date Wed, 15 Aug 2018 05:56:54 GMT
diecui1202 closed pull request #2281: [Dubbo-1713] add the log4j2 support #1713 team2
URL: https://github.com/apache/incubator-dubbo/pull/2281
 
 
   

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..3c2b069146 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -43,6 +43,15 @@
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+        </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..c3b0e39bd3 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;
 
@@ -45,6 +46,8 @@
             setLoggerAdapter(new JclLoggerAdapter());
         } else if ("log4j".equals(logger)) {
             setLoggerAdapter(new Log4jLoggerAdapter());
+        } else if ("log4j2".equals(logger)) {
+            setLoggerAdapter(new Log4j2LoggerAdapter());
         } else if ("jdk".equals(logger)) {
             setLoggerAdapter(new JdkLoggerAdapter());
         } else {
@@ -57,7 +60,11 @@
                     try {
                         setLoggerAdapter(new JclLoggerAdapter());
                     } catch (Throwable e3) {
-                        setLoggerAdapter(new JdkLoggerAdapter());
+                        try{
+                            setLoggerAdapter(new JdkLoggerAdapter());
+                        }catch (Throwable e4){
+                            setLoggerAdapter(new Log4j2LoggerAdapter());
+                        }
                     }
                 }
             }
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..47a4ebbfb7
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -0,0 +1,116 @@
+package org.apache.dubbo.common.logger.log4j2;
+
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.support.FailsafeLogger;
+
+import org.apache.logging.log4j.Level;
+
+public class Log4j2Logger implements Logger {
+    private static final String FQCN = FailsafeLogger.class.getName();
+    private static org.apache.logging.log4j.Logger logger;
+
+    public Log4j2Logger(org.apache.logging.log4j.Logger logger) {
+        this.logger = logger;
+    }
+
+    @Override
+    public void trace(String msg) {
+        logger.log(Level.TRACE, FQCN,  msg, null);
+    }
+
+    @Override
+    public void trace(Throwable e) {
+        logger.log(Level.TRACE,FQCN,  e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void trace(String msg, Throwable e) {
+        logger.log(Level.TRACE,FQCN,  msg, e);
+    }
+
+    @Override
+    public void debug(String msg) {
+        logger.log(Level.DEBUG,FQCN,  msg, null);
+    }
+
+    @Override
+    public void debug(Throwable e) {
+        logger.log(Level.DEBUG, FQCN, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void debug(String msg, Throwable e) {
+        logger.log(Level.DEBUG, FQCN, msg, e);
+    }
+
+    @Override
+    public void info(String msg) {
+        logger.log(Level.INFO, FQCN, msg, null);
+    }
+
+    @Override
+    public void info(Throwable e) {
+        logger.log(Level.INFO, FQCN, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void info(String msg, Throwable e) {
+        logger.log(Level.INFO, FQCN, msg, e);
+    }
+
+    @Override
+    public void warn(String msg) {
+        logger.log(Level.WARN, FQCN, msg, null);
+    }
+
+    @Override
+    public void warn(Throwable e) {
+        logger.log(Level.WARN, FQCN, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void warn(String msg, Throwable e) {
+        logger.log(Level.WARN, FQCN, msg, e);
+    }
+
+    @Override
+    public void error(String msg) {
+        logger.log(Level.ERROR, FQCN, msg, null);
+    }
+
+    @Override
+    public void error(Throwable e) {
+        logger.log(Level.ERROR, FQCN, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void error(String msg, Throwable e) {
+        logger.log(Level.ERROR, FQCN, 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..799ae0926c
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -0,0 +1,51 @@
+package org.apache.dubbo.common.logger.log4j2;
+
+import org.apache.dubbo.common.logger.LoggerAdapter;
+import org.apache.dubbo.common.logger.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.Level;
+
+
+import java.io.File;
+
+
+public class Log4j2LoggerAdapter implements LoggerAdapter {
+
+    private File file;
+    private Level level;
+
+
+    @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 file;
+    }
+
+    @Override
+    public void setFile(File file) {
+        this.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..32466d8ce4 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
+log4j2=org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter
 jdk=org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter
\ No newline at end of file
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..571b80c827 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,6 +44,7 @@
                 {JclLoggerAdapter.class, JclLogger.class},
                 {JdkLoggerAdapter.class, JdkLogger.class},
                 {Log4jLoggerAdapter.class, Log4jLogger.class},
+                {Log4j2LoggerAdapter.class, Log4j2Logger.class},
                 {Slf4jLoggerAdapter.class, Slf4jLogger.class}
         });
     }
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java
index 1e718dd6d0..549635ca6b 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java
@@ -41,6 +41,7 @@ public void testGetLogFile() {
         assertThat(file, is(nullValue()));
     }
 
+
     @Test
     public void testAllLogLevel() {
         for (Level targetLevel : Level.values()) {
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java
index ed87b152a0..62bdc2f9a1 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java
@@ -19,6 +19,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.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,6 +41,7 @@
                 {JclLoggerAdapter.class},
                 {JdkLoggerAdapter.class},
                 {Log4jLoggerAdapter.class},
+                {Log4j2LoggerAdapter.class},
                 {Slf4jLoggerAdapter.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..4ae8e9cade 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -105,6 +105,7 @@
         <slf4j_version>1.7.25</slf4j_version>
         <jcl_version>1.2</jcl_version>
         <log4j_version>1.2.16</log4j_version>
+        <log4j2_version>2.9.1</log4j2_version>
         <logback_version>1.2.2</logback_version>
         <embedded_redis_version>0.6</embedded_redis_version>
 
@@ -331,6 +332,16 @@
                 <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


Mime
View raw message