Skip to content

Commit

Permalink
Use local LoggerContext in Log4jLog when static field not initialized…
Browse files Browse the repository at this point in the history
… yet

Closes gh-24440
  • Loading branch information
jhoeller committed Jan 28, 2020
1 parent cf1d521 commit 844619c
Showing 1 changed file with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -530,89 +553,107 @@ 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;
}
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);
Expand Down

0 comments on commit 844619c

Please sign in to comment.