From 1762f36e7ed797747ae692ea2c78001d45b35449 Mon Sep 17 00:00:00 2001 From: Mat Booth Date: Mon, 7 Sep 2020 14:29:44 +0100 Subject: [PATCH 5/5] Remove optional dep log4j --- common/pom.xml | 15 - .../logging/InternalLoggerFactory.java | 14 +- .../util/internal/logging/Log4J2Logger.java | 143 ----- .../internal/logging/Log4J2LoggerFactory.java | 35 - .../util/internal/logging/Log4JLogger.java | 597 ------------------ .../internal/logging/Log4JLoggerFactory.java | 40 -- 6 files changed, 2 insertions(+), 842 deletions(-) delete mode 100644 common/src/main/java/io/netty/util/internal/logging/Log4J2Logger.java delete mode 100644 common/src/main/java/io/netty/util/internal/logging/Log4J2LoggerFactory.java delete mode 100644 common/src/main/java/io/netty/util/internal/logging/Log4JLogger.java delete mode 100644 common/src/main/java/io/netty/util/internal/logging/Log4JLoggerFactory.java diff --git a/common/pom.xml b/common/pom.xml index abc73161eb..60693fb81a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -63,21 +63,6 @@ commons-logging true - - log4j - log4j - true - - - org.apache.logging.log4j - log4j-api - true - - - org.apache.logging.log4j - log4j-core - test - org.mockito mockito-core diff --git a/common/src/main/java/io/netty/util/internal/logging/InternalLoggerFactory.java b/common/src/main/java/io/netty/util/internal/logging/InternalLoggerFactory.java index 508f571820..98eb4e4f4d 100644 --- a/common/src/main/java/io/netty/util/internal/logging/InternalLoggerFactory.java +++ b/common/src/main/java/io/netty/util/internal/logging/InternalLoggerFactory.java @@ -44,18 +44,8 @@ public abstract class InternalLoggerFactory { f = new Slf4JLoggerFactory(true); f.newInstance(name).debug("Using SLF4J as the default logging framework"); } catch (Throwable ignore1) { - try { - f = Log4J2LoggerFactory.INSTANCE; - f.newInstance(name).debug("Using Log4J2 as the default logging framework"); - } catch (Throwable ignore2) { - try { - f = Log4JLoggerFactory.INSTANCE; - f.newInstance(name).debug("Using Log4J as the default logging framework"); - } catch (Throwable ignore3) { - f = JdkLoggerFactory.INSTANCE; - f.newInstance(name).debug("Using java.util.logging as the default logging framework"); - } - } + f = JdkLoggerFactory.INSTANCE; + f.newInstance(name).debug("Using java.util.logging as the default logging framework"); } return f; } diff --git a/common/src/main/java/io/netty/util/internal/logging/Log4J2Logger.java b/common/src/main/java/io/netty/util/internal/logging/Log4J2Logger.java deleted file mode 100644 index 5c3593f203..0000000000 --- a/common/src/main/java/io/netty/util/internal/logging/Log4J2Logger.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright 2016 The Netty Project - * - * The Netty Project 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 io.netty.util.internal.logging; - - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.spi.ExtendedLoggerWrapper; - -import java.security.AccessController; -import java.security.PrivilegedAction; - -import static io.netty.util.internal.logging.AbstractInternalLogger.EXCEPTION_MESSAGE; - -class Log4J2Logger extends ExtendedLoggerWrapper implements InternalLogger { - - private static final long serialVersionUID = 5485418394879791397L; - private static final boolean VARARGS_ONLY; - - static { - // Older Log4J2 versions have only log methods that takes the format + varargs. So we should not use - // Log4J2 if the version is too old. - // See https://github.com/netty/netty/issues/8217 - VARARGS_ONLY = AccessController.doPrivileged(new PrivilegedAction() { - @Override - public Boolean run() { - try { - Logger.class.getMethod("debug", String.class, Object.class); - return false; - } catch (NoSuchMethodException ignore) { - // Log4J2 version too old. - return true; - } catch (SecurityException ignore) { - // We could not detect the version so we will use Log4J2 if its on the classpath. - return false; - } - } - }); - } - - Log4J2Logger(Logger logger) { - super((ExtendedLogger) logger, logger.getName(), logger.getMessageFactory()); - if (VARARGS_ONLY) { - throw new UnsupportedOperationException("Log4J2 version mismatch"); - } - } - - @Override - public String name() { - return getName(); - } - - @Override - public void trace(Throwable t) { - log(Level.TRACE, EXCEPTION_MESSAGE, t); - } - - @Override - public void debug(Throwable t) { - log(Level.DEBUG, EXCEPTION_MESSAGE, t); - } - - @Override - public void info(Throwable t) { - log(Level.INFO, EXCEPTION_MESSAGE, t); - } - - @Override - public void warn(Throwable t) { - log(Level.WARN, EXCEPTION_MESSAGE, t); - } - - @Override - public void error(Throwable t) { - log(Level.ERROR, EXCEPTION_MESSAGE, t); - } - - @Override - public boolean isEnabled(InternalLogLevel level) { - return isEnabled(toLevel(level)); - } - - @Override - public void log(InternalLogLevel level, String msg) { - log(toLevel(level), msg); - } - - @Override - public void log(InternalLogLevel level, String format, Object arg) { - log(toLevel(level), format, arg); - } - - @Override - public void log(InternalLogLevel level, String format, Object argA, Object argB) { - log(toLevel(level), format, argA, argB); - } - - @Override - public void log(InternalLogLevel level, String format, Object... arguments) { - log(toLevel(level), format, arguments); - } - - @Override - public void log(InternalLogLevel level, String msg, Throwable t) { - log(toLevel(level), msg, t); - } - - @Override - public void log(InternalLogLevel level, Throwable t) { - log(toLevel(level), EXCEPTION_MESSAGE, t); - } - - private static Level toLevel(InternalLogLevel level) { - switch (level) { - case INFO: - return Level.INFO; - case DEBUG: - return Level.DEBUG; - case WARN: - return Level.WARN; - case ERROR: - return Level.ERROR; - case TRACE: - return Level.TRACE; - default: - throw new Error(); - } - } -} diff --git a/common/src/main/java/io/netty/util/internal/logging/Log4J2LoggerFactory.java b/common/src/main/java/io/netty/util/internal/logging/Log4J2LoggerFactory.java deleted file mode 100644 index 8b895fbc07..0000000000 --- a/common/src/main/java/io/netty/util/internal/logging/Log4J2LoggerFactory.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016 The Netty Project - * - * The Netty Project 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 io.netty.util.internal.logging; - -import org.apache.logging.log4j.LogManager; - -public final class Log4J2LoggerFactory extends InternalLoggerFactory { - - public static final InternalLoggerFactory INSTANCE = new Log4J2LoggerFactory(); - - /** - * @deprecated Use {@link #INSTANCE} instead. - */ - @Deprecated - public Log4J2LoggerFactory() { - } - - @Override - public InternalLogger newInstance(String name) { - return new Log4J2Logger(LogManager.getLogger(name)); - } -} diff --git a/common/src/main/java/io/netty/util/internal/logging/Log4JLogger.java b/common/src/main/java/io/netty/util/internal/logging/Log4JLogger.java deleted file mode 100644 index 27b9130a08..0000000000 --- a/common/src/main/java/io/netty/util/internal/logging/Log4JLogger.java +++ /dev/null @@ -1,597 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project 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. - */ -/** - * Copyright (c) 2004-2011 QOS.ch - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -package io.netty.util.internal.logging; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; - -/** - * Apache Log4J - * logger. - */ -class Log4JLogger extends AbstractInternalLogger { - - private static final long serialVersionUID = 2851357342488183058L; - - final transient Logger logger; - - /** - * Following the pattern discussed in pages 162 through 168 of "The complete - * log4j manual". - */ - static final String FQCN = Log4JLogger.class.getName(); - - // Does the log4j version in use recognize the TRACE level? - // The trace level was introduced in log4j 1.2.12. - final boolean traceCapable; - - Log4JLogger(Logger logger) { - super(logger.getName()); - this.logger = logger; - traceCapable = isTraceCapable(); - } - - private boolean isTraceCapable() { - try { - logger.isTraceEnabled(); - return true; - } catch (NoSuchMethodError ignored) { - return false; - } - } - - /** - * Is this logger instance enabled for the TRACE level? - * - * @return True if this Logger is enabled for level TRACE, false otherwise. - */ - @Override - public boolean isTraceEnabled() { - if (traceCapable) { - return logger.isTraceEnabled(); - } else { - return logger.isDebugEnabled(); - } - } - - /** - * Log a message object at level TRACE. - * - * @param msg - * - the message object to be logged - */ - @Override - public void trace(String msg) { - logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, null); - } - - /** - * Log a message at level TRACE according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for level TRACE. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - @Override - public void trace(String format, Object arg) { - if (isTraceEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, arg); - logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft - .getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level TRACE according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the TRACE level. - *

- * - * @param format - * the format string - * @param argA - * the first argument - * @param argB - * the second argument - */ - @Override - public void trace(String format, Object argA, Object argB) { - if (isTraceEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft - .getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level TRACE according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the TRACE level. - *

- * - * @param format - * the format string - * @param arguments - * an array of arguments - */ - @Override - public void trace(String format, Object... arguments) { - if (isTraceEnabled()) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments); - logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, ft - .getMessage(), ft.getThrowable()); - } - } - - /** - * Log an exception (throwable) at level TRACE with an accompanying message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - @Override - public void trace(String msg, Throwable t) { - logger.log(FQCN, traceCapable ? Level.TRACE : Level.DEBUG, msg, t); - } - - /** - * Is this logger instance enabled for the DEBUG level? - * - * @return True if this Logger is enabled for level DEBUG, false otherwise. - */ - @Override - public boolean isDebugEnabled() { - return logger.isDebugEnabled(); - } - - /** - * Log a message object at level DEBUG. - * - * @param msg - * - the message object to be logged - */ - @Override - public void debug(String msg) { - logger.log(FQCN, Level.DEBUG, msg, null); - } - - /** - * Log a message at level DEBUG according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for level DEBUG. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - @Override - public void debug(String format, Object arg) { - if (logger.isDebugEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, arg); - logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level DEBUG according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the DEBUG level. - *

- * - * @param format - * the format string - * @param argA - * the first argument - * @param argB - * the second argument - */ - @Override - public void debug(String format, Object argA, Object argB) { - if (logger.isDebugEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level DEBUG according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the DEBUG level. - *

- * - * @param format - * the format string - * @param arguments an array of arguments - */ - @Override - public void debug(String format, Object... arguments) { - if (logger.isDebugEnabled()) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments); - logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log an exception (throwable) at level DEBUG with an accompanying message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - @Override - public void debug(String msg, Throwable t) { - logger.log(FQCN, Level.DEBUG, msg, t); - } - - /** - * Is this logger instance enabled for the INFO level? - * - * @return True if this Logger is enabled for the INFO level, false otherwise. - */ - @Override - public boolean isInfoEnabled() { - return logger.isInfoEnabled(); - } - - /** - * Log a message object at the INFO level. - * - * @param msg - * - the message object to be logged - */ - @Override - public void info(String msg) { - logger.log(FQCN, Level.INFO, msg, null); - } - - /** - * Log a message at level INFO according to the specified format and argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - @Override - public void info(String format, Object arg) { - if (logger.isInfoEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, arg); - logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at the INFO level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param argA - * the first argument - * @param argB - * the second argument - */ - @Override - public void info(String format, Object argA, Object argB) { - if (logger.isInfoEnabled()) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level INFO according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - @Override - public void info(String format, Object... argArray) { - if (logger.isInfoEnabled()) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log an exception (throwable) at the INFO level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - @Override - public void info(String msg, Throwable t) { - logger.log(FQCN, Level.INFO, msg, t); - } - - /** - * Is this logger instance enabled for the WARN level? - * - * @return True if this Logger is enabled for the WARN level, false otherwise. - */ - @Override - public boolean isWarnEnabled() { - return logger.isEnabledFor(Level.WARN); - } - - /** - * Log a message object at the WARN level. - * - * @param msg - * - the message object to be logged - */ - @Override - public void warn(String msg) { - logger.log(FQCN, Level.WARN, msg, null); - } - - /** - * Log a message at the WARN level according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - @Override - public void warn(String format, Object arg) { - if (logger.isEnabledFor(Level.WARN)) { - FormattingTuple ft = MessageFormatter.format(format, arg); - logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at the WARN level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param argA - * the first argument - * @param argB - * the second argument - */ - @Override - public void warn(String format, Object argA, Object argB) { - if (logger.isEnabledFor(Level.WARN)) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level WARN according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - @Override - public void warn(String format, Object... argArray) { - if (logger.isEnabledFor(Level.WARN)) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - logger.log(FQCN, Level.WARN, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log an exception (throwable) at the WARN level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - @Override - public void warn(String msg, Throwable t) { - logger.log(FQCN, Level.WARN, msg, t); - } - - /** - * Is this logger instance enabled for level ERROR? - * - * @return True if this Logger is enabled for level ERROR, false otherwise. - */ - @Override - public boolean isErrorEnabled() { - return logger.isEnabledFor(Level.ERROR); - } - - /** - * Log a message object at the ERROR level. - * - * @param msg - * - the message object to be logged - */ - @Override - public void error(String msg) { - logger.log(FQCN, Level.ERROR, msg, null); - } - - /** - * Log a message at the ERROR level according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - @Override - public void error(String format, Object arg) { - if (logger.isEnabledFor(Level.ERROR)) { - FormattingTuple ft = MessageFormatter.format(format, arg); - logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at the ERROR level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param argA - * the first argument - * @param argB - * the second argument - */ - @Override - public void error(String format, Object argA, Object argB) { - if (logger.isEnabledFor(Level.ERROR)) { - FormattingTuple ft = MessageFormatter.format(format, argA, argB); - logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log a message at level ERROR according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - @Override - public void error(String format, Object... argArray) { - if (logger.isEnabledFor(Level.ERROR)) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - logger.log(FQCN, Level.ERROR, ft.getMessage(), ft.getThrowable()); - } - } - - /** - * Log an exception (throwable) at the ERROR level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - @Override - public void error(String msg, Throwable t) { - logger.log(FQCN, Level.ERROR, msg, t); - } -} diff --git a/common/src/main/java/io/netty/util/internal/logging/Log4JLoggerFactory.java b/common/src/main/java/io/netty/util/internal/logging/Log4JLoggerFactory.java deleted file mode 100644 index 399a279df3..0000000000 --- a/common/src/main/java/io/netty/util/internal/logging/Log4JLoggerFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project 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 io.netty.util.internal.logging; - -import org.apache.log4j.Logger; - -/** - * Logger factory which creates an - * Apache Log4J - * logger. - */ -public class Log4JLoggerFactory extends InternalLoggerFactory { - - public static final InternalLoggerFactory INSTANCE = new Log4JLoggerFactory(); - - /** - * @deprecated Use {@link #INSTANCE} instead. - */ - @Deprecated - public Log4JLoggerFactory() { - } - - @Override - public InternalLogger newInstance(String name) { - return new Log4JLogger(Logger.getLogger(name)); - } -} -- 2.26.2