From af2a538d3dfada0f719fc07e36919148d8d97c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=A9=E7=8E=A9=E8=B7=91=E8=B7=91?= <18244217741@163.com> Date: Fri, 17 Aug 2018 15:43:50 +0800 Subject: [PATCH] add log4j2 extension (#2279) * add log4j2 for dubbo logging, #1713 * remove unused code, add provided scope to log4j2 --- dubbo-common/pom.xml | 10 ++ .../dubbo/common/logger/LoggerFactory.java | 11 +- .../common/logger/log4j2/Log4j2Logger.java | 128 ++++++++++++++++++ .../logger/log4j2/Log4j2LoggerAdapter.java | 107 +++++++++++++++ ...g.apache.dubbo.common.logger.LoggerAdapter | 3 +- .../common/logger/LoggerAdapterTest.java | 5 +- .../common/logger/slf4j/Slf4jLoggerTest.java | 1 - dubbo-dependencies-bom/pom.xml | 13 ++ 8 files changed, 273 insertions(+), 5 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2Logger.java create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java 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 @@ log4j log4j + + org.apache.logging.log4j + log4j-api + provided + + + org.apache.logging.log4j + log4j-core + provided + org.javassist javassist 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.extension.ExtensionLoader; 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 @@ public class LoggerFactory { 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 @@ public class LoggerFactory { 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.JdkLogger; 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 @@ public class LoggerAdapterTest { {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 class Slf4jLoggerTest { 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 @@ 1.2 1.2.16 1.2.2 + 2.11.1 + 0.6 2.2.7 @@ -331,6 +333,17 @@ logback-classic ${logback_version} + + org.apache.logging.log4j + log4j-api + ${log4j2_version} + + + org.apache.logging.log4j + log4j-core + ${log4j2_version} + +