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 #2266: [Dubbo-1713] add the log4j2 support #1713 team3
Date Wed, 15 Aug 2018 05:56:53 GMT
diecui1202 closed pull request #2266: [Dubbo-1713] add the log4j2 support #1713 team3
URL: https://github.com/apache/incubator-dubbo/pull/2266
 
 
   

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..2d0ad404d9 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-core</artifactId>
+            <version>2.5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>2.5</version>
+        </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..b0054120cd 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 {
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..c0c3a2f3cd
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -0,0 +1,118 @@
+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.log(FQCN, Level.TRACE, msg, null);
+        logger.log(Level.TRACE,msg);
+    }
+
+    @Override
+    public void trace(Throwable e) {
+        logger.log(Level.TRACE,e==null?null:e.getMessage());
+    }
+
+    @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..8dea88411d
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -0,0 +1,111 @@
+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.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;
+    @SuppressWarnings("unchecked")
+    public Log4j2LoggerAdapter() {
+        try {
+            org.apache.logging.log4j.Logger logger = LogManager.getLogger();
+            if (logger != null) {
+                org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger)
logger;
+                Map<String, Appender> appenders = coreLogger.getAppenders();
+                if (appenders != null) {
+                    for (Appender appender : appenders.values()) {
+                        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) {
+//        LogManager.getRootLogger().setLevel(toLog4j2Level(level));
+//        LogManager.getRootLogger()
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        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) {
+
+    }
+
+}
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-demo/dubbo-demo-consumer/src/main/resources/log4j2.xml b/dubbo-demo/dubbo-demo-consumer/src/main/resources/log4j2.xml
new file mode 100644
index 0000000000..5c6e9384b5
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-consumer/src/main/resources/log4j2.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="INFO">
+    <Appenders>
+         <Console name="Console" target="SYSTEM_OUT">
+           <PatternLayout pattern="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n"/>
+         </Console>
+       </Appenders>
+       <Loggers>
+         <Root level="info">
+           <AppenderRef ref="Console"/>
+         </Root>
+       </Loggers>
+</Configuration>
diff --git a/dubbo-demo/dubbo-demo-provider/src/main/resources/log4j2.xml b/dubbo-demo/dubbo-demo-provider/src/main/resources/log4j2.xml
new file mode 100644
index 0000000000..5c6e9384b5
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-provider/src/main/resources/log4j2.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="INFO">
+    <Appenders>
+         <Console name="Console" target="SYSTEM_OUT">
+           <PatternLayout pattern="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n"/>
+         </Console>
+       </Appenders>
+       <Loggers>
+         <Root level="info">
+           <AppenderRef ref="Console"/>
+         </Root>
+       </Loggers>
+</Configuration>


 

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