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 #2285: Dubbo-BaiJi_269_team_9_issue_1713
Date Wed, 15 Aug 2018 05:56:53 GMT
diecui1202 closed pull request #2285: Dubbo-BaiJi_269_team_9_issue_1713
URL: https://github.com/apache/incubator-dubbo/pull/2285
 
 
   

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..ed8f710b08 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -43,6 +43,14 @@
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</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..ac3c53f966 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,17 +48,23 @@
             setLoggerAdapter(new Log4jLoggerAdapter());
         } else if ("jdk".equals(logger)) {
             setLoggerAdapter(new JdkLoggerAdapter());
+        } else if("log4j2".equals(logger)){
+            setLoggerAdapter(new Log4j2LoggerAdapter());
         } else {
             try {
-                setLoggerAdapter(new Log4jLoggerAdapter());
+                setLoggerAdapter(new Slf4jLoggerAdapter());
             } catch (Throwable e1) {
                 try {
-                    setLoggerAdapter(new Slf4jLoggerAdapter());
+                    setLoggerAdapter(new Log4jLoggerAdapter());
                 } catch (Throwable e2) {
                     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..647d191653
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -0,0 +1,114 @@
+package org.apache.dubbo.common.logger.log4j2;
+
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.logging.log4j.Level;
+
+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.log(Level.TRACE, msg);
+    }
+
+    @Override
+    public void trace(Throwable e) {
+        logger.log(Level.TRACE, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void trace(String msg, Throwable e) {
+        logger.log(Level.TRACE, msg, e);
+    }
+
+    @Override
+    public void debug(String msg) {
+        logger.log(Level.DEBUG, msg);
+    }
+
+    @Override
+    public void debug(Throwable e) {
+        logger.log(Level.DEBUG, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void debug(String msg, Throwable e) {
+        logger.log(Level.DEBUG, msg, e);
+    }
+
+    @Override
+    public void info(String msg) {
+        logger.log(Level.INFO, msg);
+    }
+
+    @Override
+    public void info(Throwable e) {
+        logger.log(Level.INFO, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void info(String msg, Throwable e) {
+        logger.log(Level.INFO, msg, e);
+    }
+
+    @Override
+    public void warn(String msg) {
+        logger.log(Level.WARN, msg);
+    }
+
+    @Override
+    public void warn(Throwable e) {
+        logger.log(Level.WARN, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void warn(String msg, Throwable e) {
+        logger.log(Level.WARN, msg, e);
+    }
+
+    @Override
+    public void error(String msg) {
+        logger.log(Level.ERROR, msg);
+    }
+
+    @Override
+    public void error(Throwable e) {
+        logger.log(Level.ERROR, e == null ? null : e.getMessage(), e);
+    }
+
+    @Override
+    public void error(String msg, Throwable e) {
+        logger.log(Level.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..117962b52d
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -0,0 +1,104 @@
+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 org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.appender.FileAppender;
+import org.apache.logging.log4j.core.config.Configurator;
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Map;
+
+public class Log4j2LoggerAdapter implements LoggerAdapter {
+
+    private File file;
+
+    public Log4j2LoggerAdapter() {
+        try {
+            org.apache.logging.log4j.Logger logger = LogManager.getRootLogger();
+            org.apache.logging.log4j.core.Logger loggerImpl = (org.apache.logging.log4j.core.Logger)
logger;
+            if (logger != null) {
+                Map<String, Appender> appenderMap = loggerImpl.getAppenders();
+                Iterator iter = appenderMap.entrySet().iterator();
+                while (iter.hasNext()) {
+                    Map.Entry entry = (Map.Entry) iter.next();
+                    Appender appender = (Appender) entry.getValue();
+                    if(appender instanceof FileAppender){
+                        FileAppender fileAppender = (FileAppender) appender;
+                        String filename = fileAppender.getFileName();
+                        file = new File(filename);
+                        break;
+                    }
+                }
+            }
+        } catch (Throwable t) {
+        }
+    }
+
+    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;
+        // if (level == Level.OFF)
+        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;
+        // if (level == org.apache.log4j.Level.OFF)
+        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 fromLog4j2Level(LogManager.getRootLogger().getLevel());
+    }
+
+    @Override
+    public void setLevel(Level level) {
+        Configurator.setRootLevel(toLog4j2Level(level));
+    }
+
+    @Override
+    public File getFile() {
+        return file;
+    }
+
+    @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..47cdfd5e32 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
\ 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..a3743db9ef 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/LoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java
index ed87b152a0..cd7b784d5b 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,7 +41,8 @@
                 {JclLoggerAdapter.class},
                 {JdkLoggerAdapter.class},
                 {Log4jLoggerAdapter.class},
-                {Slf4jLoggerAdapter.class}
+                {Slf4jLoggerAdapter.class},
+                {Log4j2LoggerAdapter.class}
         });
     }
 
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 29ca7a7c5e..7529b17d5f 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.5</log4j2_version>
         <logback_version>1.2.2</logback_version>
         <embedded_redis_version>0.6</embedded_redis_version>
 
@@ -326,6 +327,16 @@
                 <artifactId>log4j</artifactId>
                 <version>${log4j_version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-core</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-api</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
             <dependency>
                 <groupId>ch.qos.logback</groupId>
                 <artifactId>logback-classic</artifactId>


 

----------------------------------------------------------------
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