diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java b/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java index d7cfff5cf4..b0c62523b2 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/Logger.java @@ -67,7 +67,7 @@ public final class Logger /** * The children of this logger. A logger may have zero or more children. */ - transient private List childrenList; + private final transient List childrenList = new CopyOnWriteArrayList<>(); /** * It is assumed that once the 'aai' variable is set to a non-null value, it @@ -75,7 +75,7 @@ public final class Logger * 'aai variable is set is within the addAppender method. This method is * synchronized on 'this' (Logger) protecting against simultaneous * re-configuration of this logger (a very unlikely scenario). - * + * *

* It is further assumed that the AppenderAttachableImpl is responsible for its * internal synchronization and thread safety. Thus, we can get away with *not* @@ -130,9 +130,6 @@ private boolean isRootLogger() { } Logger getChildByName(final String childName) { - if (childrenList == null) { - return null; - } for (final Logger child : childrenList) { if (childName.equals(child.getName())) { return child; @@ -159,11 +156,9 @@ public synchronized void setLevel(Level newLevel) { effectiveLevelInt = newLevel.levelInt; } - if (childrenList != null) { - for (Logger childLogger : childrenList) { - // tell child to handle parent levelInt change - childLogger.handleParentLevelChange(effectiveLevelInt); - } + for (Logger childLogger : childrenList) { + // tell child to handle parent levelInt change + childLogger.handleParentLevelChange(effectiveLevelInt); } // inform listeners loggerContext.fireOnLevelChange(this, newLevel); @@ -182,10 +177,8 @@ private synchronized void handleParentLevelChange(int newParentLevelInt) { effectiveLevelInt = newParentLevelInt; // propagate the parent levelInt change to this logger's children - if (childrenList != null) { - for (Logger childLogger : childrenList) { - childLogger.handleParentLevelChange(newParentLevelInt); - } + for (Logger childLogger : childrenList) { + childLogger.handleParentLevelChange(newParentLevelInt); } } } @@ -241,7 +234,7 @@ public Appender getAppender(String name) { /** * Invoke all the appenders of this logger. - * + * * @param event The event to log */ public void callAppenders(ILoggingEvent event) { @@ -280,11 +273,11 @@ public boolean detachAppender(Appender appender) { * Create a child of this logger by suffix, that is, the part of the name * extending this logger. For example, if this logger is named "x.y" and the * lastPart is "z", then the created child logger will be named "x.y.z". - * + * *

* IMPORTANT: Calls to this method must be within a synchronized block on this * logger. - * + * * @param lastPart the suffix (i.e. last part) of the child logger name. This * parameter may not include dots, i.e. the logger separator * character. @@ -297,9 +290,6 @@ Logger createChildByLastNamePart(final String lastPart) { "Child name [" + lastPart + " passed as parameter, may not include [" + CoreConstants.DOT + "]"); } - if (childrenList == null) { - childrenList = new CopyOnWriteArrayList(); - } Logger childLogger; if (this.isRootLogger()) { childLogger = new Logger(lastPart, this, this.loggerContext); @@ -324,9 +314,6 @@ void recursiveReset() { detachAndStopAllAppenders(); localLevelReset(); additive = true; - if (childrenList == null) { - return; - } for (Logger childLogger : childrenList) { childLogger.recursiveReset(); } @@ -339,9 +326,6 @@ synchronized Logger createChildByName(final String childName) { + " passed as parameter, may not include '.' after index" + (this.name.length() + 1)); } - if (childrenList == null) { - childrenList = new CopyOnWriteArrayList<>(); - } final Logger childLogger = new Logger(childName, this, this.loggerContext); childrenList.add(childLogger); childLogger.effectiveLevelInt = this.effectiveLevelInt; @@ -729,11 +713,11 @@ public String toString() { /** * Method that calls the attached TurboFilter objects based on the logger and * the level. - * + * * It is used by isYYYEnabled() methods. - * + * * It returns the typical FilterReply values: ACCEPT, NEUTRAL or DENY. - * + * * @param level * @return the reply given by the TurboFilters */ @@ -743,7 +727,7 @@ private FilterReply callTurboFilters(Marker marker, Level level) { /** * Return the context for this logger. - * + * * @return the context */ public LoggerContext getLoggerContext() { @@ -752,7 +736,7 @@ public LoggerContext getLoggerContext() { /** * Creates a {@link LoggingEventBuilder} of type {@link DefaultLoggingEventBuilder}. - * + * * @since 1.3 */ @Override @@ -806,7 +790,7 @@ public void log(org.slf4j.event.LoggingEvent slf4jEvent) { * After serialization, the logger instance does not know its LoggerContext. The * best we can do here, is to return a logger with the same name returned by * org.slf4j.LoggerFactory. - * + * * @return Logger instance with the same name * @throws ObjectStreamException */