Skip to content

Commit

Permalink
make childrenList initialize early
Browse files Browse the repository at this point in the history
Signed-off-by: Wenjun Ruan <wenjun@apache.org>
  • Loading branch information
ruanwenjun committed Oct 13, 2024
1 parent 9015b2e commit 4b8167c
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions logback-classic/src/main/java/ch/qos/logback/classic/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public final class Logger
/**
* The children of this logger. A logger may have zero or more children.
*/
transient private List<Logger> childrenList;
private final transient List<Logger> childrenList = new CopyOnWriteArrayList<>();

/**
* It is assumed that once the 'aai' variable is set to a non-null value, it
* will never be reset to null. it is further assumed that only place where the
* '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).
*
*
* <p>
* It is further assumed that the AppenderAttachableImpl is responsible for its
* internal synchronization and thread safety. Thus, we can get away with *not*
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -241,7 +234,7 @@ public Appender<ILoggingEvent> getAppender(String name) {

/**
* Invoke all the appenders of this logger.
*
*
* @param event The event to log
*/
public void callAppenders(ILoggingEvent event) {
Expand Down Expand Up @@ -280,11 +273,11 @@ public boolean detachAppender(Appender<ILoggingEvent> 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".
*
*
* <p>
* 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.
Expand All @@ -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>();
}
Logger childLogger;
if (this.isRootLogger()) {
childLogger = new Logger(lastPart, this, this.loggerContext);
Expand All @@ -324,9 +314,6 @@ void recursiveReset() {
detachAndStopAllAppenders();
localLevelReset();
additive = true;
if (childrenList == null) {
return;
}
for (Logger childLogger : childrenList) {
childLogger.recursiveReset();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -743,7 +727,7 @@ private FilterReply callTurboFilters(Marker marker, Level level) {

/**
* Return the context for this logger.
*
*
* @return the context
*/
public LoggerContext getLoggerContext() {
Expand All @@ -752,7 +736,7 @@ public LoggerContext getLoggerContext() {

/**
* Creates a {@link LoggingEventBuilder} of type {@link DefaultLoggingEventBuilder}.
*
*
* @since 1.3
*/
@Override
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 4b8167c

Please sign in to comment.