From 844619ccf1c7cdd4c3711eb1277f62265ca15e93 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 28 Jan 2020 21:30:52 +0100 Subject: [PATCH] Use local LoggerContext in Log4jLog when static field not initialized yet Closes gh-24440 --- .../apache/commons/logging/LogFactory.java | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/spring-jcl/src/main/java/org/apache/commons/logging/LogFactory.java b/spring-jcl/src/main/java/org/apache/commons/logging/LogFactory.java index ff5027c96a9f..569bb761951f 100644 --- a/spring-jcl/src/main/java/org/apache/commons/logging/LogFactory.java +++ b/spring-jcl/src/main/java/org/apache/commons/logging/LogFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -201,7 +201,12 @@ private static class Log4jLog implements Log, Serializable { private final ExtendedLogger logger; public Log4jLog(String name) { - this.logger = loggerContext.getLogger(name); + LoggerContext context = loggerContext; + if (context == null) { + // Circular call in early-init scenario -> static field not initialized yet + context = LogManager.getContext(Log4jLog.class.getClassLoader(), false); + } + this.logger = context.getLogger(name); } @Override @@ -324,92 +329,110 @@ public Slf4jLog(T logger) { this.logger = logger; } + @Override public boolean isFatalEnabled() { return isErrorEnabled(); } + @Override public boolean isErrorEnabled() { return this.logger.isErrorEnabled(); } + @Override public boolean isWarnEnabled() { return this.logger.isWarnEnabled(); } + @Override public boolean isInfoEnabled() { return this.logger.isInfoEnabled(); } + @Override public boolean isDebugEnabled() { return this.logger.isDebugEnabled(); } + @Override public boolean isTraceEnabled() { return this.logger.isTraceEnabled(); } + @Override public void fatal(Object message) { error(message); } + @Override public void fatal(Object message, Throwable exception) { error(message, exception); } + @Override public void error(Object message) { if (message instanceof String || this.logger.isErrorEnabled()) { this.logger.error(String.valueOf(message)); } } + @Override public void error(Object message, Throwable exception) { if (message instanceof String || this.logger.isErrorEnabled()) { this.logger.error(String.valueOf(message), exception); } } + @Override public void warn(Object message) { if (message instanceof String || this.logger.isWarnEnabled()) { this.logger.warn(String.valueOf(message)); } } + @Override public void warn(Object message, Throwable exception) { if (message instanceof String || this.logger.isWarnEnabled()) { this.logger.warn(String.valueOf(message), exception); } } + @Override public void info(Object message) { if (message instanceof String || this.logger.isInfoEnabled()) { this.logger.info(String.valueOf(message)); } } + @Override public void info(Object message, Throwable exception) { if (message instanceof String || this.logger.isInfoEnabled()) { this.logger.info(String.valueOf(message), exception); } } + @Override public void debug(Object message) { if (message instanceof String || this.logger.isDebugEnabled()) { this.logger.debug(String.valueOf(message)); } } + @Override public void debug(Object message, Throwable exception) { if (message instanceof String || this.logger.isDebugEnabled()) { this.logger.debug(String.valueOf(message), exception); } } + @Override public void trace(Object message) { if (message instanceof String || this.logger.isTraceEnabled()) { this.logger.trace(String.valueOf(message)); } } + @Override public void trace(Object message, Throwable exception) { if (message instanceof String || this.logger.isTraceEnabled()) { this.logger.trace(String.valueOf(message), exception); @@ -530,80 +553,98 @@ public JavaUtilLog(String name) { this.logger = java.util.logging.Logger.getLogger(name); } + @Override public boolean isFatalEnabled() { return isErrorEnabled(); } + @Override public boolean isErrorEnabled() { return this.logger.isLoggable(java.util.logging.Level.SEVERE); } + @Override public boolean isWarnEnabled() { return this.logger.isLoggable(java.util.logging.Level.WARNING); } + @Override public boolean isInfoEnabled() { return this.logger.isLoggable(java.util.logging.Level.INFO); } + @Override public boolean isDebugEnabled() { return this.logger.isLoggable(java.util.logging.Level.FINE); } + @Override public boolean isTraceEnabled() { return this.logger.isLoggable(java.util.logging.Level.FINEST); } + @Override public void fatal(Object message) { error(message); } + @Override public void fatal(Object message, Throwable exception) { error(message, exception); } + @Override public void error(Object message) { log(java.util.logging.Level.SEVERE, message, null); } + @Override public void error(Object message, Throwable exception) { log(java.util.logging.Level.SEVERE, message, exception); } + @Override public void warn(Object message) { log(java.util.logging.Level.WARNING, message, null); } + @Override public void warn(Object message, Throwable exception) { log(java.util.logging.Level.WARNING, message, exception); } + @Override public void info(Object message) { log(java.util.logging.Level.INFO, message, null); } + @Override public void info(Object message, Throwable exception) { log(java.util.logging.Level.INFO, message, exception); } + @Override public void debug(Object message) { log(java.util.logging.Level.FINE, message, null); } + @Override public void debug(Object message, Throwable exception) { log(java.util.logging.Level.FINE, message, exception); } + @Override public void trace(Object message) { log(java.util.logging.Level.FINEST, message, null); } + @Override public void trace(Object message, Throwable exception) { log(java.util.logging.Level.FINEST, message, exception); } private void log(java.util.logging.Level level, Object message, Throwable exception) { - if (logger.isLoggable(level)) { + if (this.logger.isLoggable(level)) { LogRecord rec; if (message instanceof LogRecord) { rec = (LogRecord) message; @@ -611,8 +652,8 @@ private void log(java.util.logging.Level level, Object message, Throwable except else { rec = new LocationResolvingLogRecord(level, String.valueOf(message)); rec.setLoggerName(this.name); - rec.setResourceBundleName(logger.getResourceBundleName()); - rec.setResourceBundle(logger.getResourceBundle()); + rec.setResourceBundleName(this.logger.getResourceBundleName()); + rec.setResourceBundle(this.logger.getResourceBundle()); rec.setThrown(exception); } logger.log(rec);