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 #2250: Fix Issue #1713. Add support for log4j2. Team4
Date Wed, 15 Aug 2018 05:56:52 GMT
diecui1202 closed pull request #2250: Fix Issue #1713. Add support for log4j2. Team4 
URL: https://github.com/apache/incubator-dubbo/pull/2250
 
 
   

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..efe5d5c864 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -67,5 +67,15 @@
             <groupId>de.ruedigermoeller</groupId>
             <artifactId>fst</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>2.11.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <version>2.11.1</version>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
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..f931e84d97 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
@@ -21,6 +21,7 @@
 import org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter;
 import org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter;
 import org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter;
+import org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter;
 import org.apache.dubbo.common.logger.support.FailsafeLogger;
 
 import java.io.File;
@@ -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());
@@ -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..af42beaeeb
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -0,0 +1,91 @@
+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 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.log(Level.TRACE, e);
+    }
+    @Override
+    public void trace(String msg, Throwable e) {
+        logger.log(Level.TRACE, msg, e);
+    }
+    @Override
+    public void debug(String msg) {
+        logger.debug(msg);
+    }
+    @Override
+    public void debug(Throwable e) {
+        logger.log(Level.DEBUG, e);
+    }
+    @Override
+    public void debug(String msg, Throwable e) {
+        logger.log(Level.DEBUG, msg, e);
+    }
+    @Override
+    public void info(String msg) {
+        logger.info(msg);
+    }
+    @Override
+    public void info(Throwable e) {
+        logger.log(Level.INFO, e);
+    }
+    @Override
+    public void info(String msg, Throwable e) {
+        logger.log(Level.INFO, msg, e);
+    }
+    @Override
+    public void warn(String msg) {
+        logger.warn(msg);
+    }
+    @Override
+    public void warn(Throwable e) {
+        logger.log(Level.WARN, e);
+    }
+    @Override
+    public void warn(String msg, Throwable e) {
+        logger.log(Level.WARN, msg, e);
+    }
+    @Override
+    public void error(String msg) {
+        logger.error(msg);
+    }
+    @Override
+    public void error(Throwable e) {
+        logger.log(Level.ERROR, 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();
+    }
+}
\ No newline at end of file
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..72e21ccfa9
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -0,0 +1,95 @@
+package org.apache.dubbo.common.logger.log4j2;
+import org.apache.logging.log4j.LogManager;
+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.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.FileAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import java.io.File;
+import java.util.Map;
+public class Log4j2LoggerAdapter implements LoggerAdapter {
+    private File file;
+    public Log4j2LoggerAdapter() {
+        try {
+            org.apache.logging.log4j.Logger logger = LogManager.getLogger();
+            if (logger != null) {
+                Map<String, Appender> appenders = ((org.apache.logging.log4j.core.Logger)
logger).getAppenders();
+                if (appenders != null) {
+                    for(Map.Entry<String, Appender> entry: appenders.entrySet()) {
+                        if (entry.getValue() instanceof FileAppender) {
+                            FileAppender fileAppender = (FileAppender) entry.getValue();
+                            String filename = fileAppender.getFileName();
+                            file = new File(filename);
+                            break;
+                        }
+                    }
+                }
+            }
+        } catch (Throwable t) {
+        }
+    }
+    @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) {
+        // log4j2 didn't provide a setLevel method in LogManager.getRootLogger() and in Log4J
Core.
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        // use reflection to set RootLogger Level
+        Configuration config = ctx.getConfiguration();
+        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
+        loggerConfig.setLevel(toLog4j2Level(level));
+        ctx.updateLoggers();
+    }
+    @Override
+    public File getFile() {
+        return file;
+    }
+    @Override
+    public void setFile(File file) {
+        this.file = file;
+    }
+    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;
+        // if (level == org.apache.log4j.Level.OFF)
+        return Level.OFF;
+    }
+}
\ No newline at end of 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}
         });
     }
 


 

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