Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of publication #44

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/org/apache/log4j/JBossAppenderHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -27,12 +28,28 @@ protected JBossAppenderHandler(final Logger logger) {

@Override
protected void doPublish(final ExtLogRecord record) {
final LoggingEvent event = new LoggingEvent(record, JBossLogManagerFacade.getLogger(logger));
final LoggingEvent event = new LoggingEvent(record, logger.getLogContext());
final List<Appender> appenders = Appenders.getAppenderList(logger);
for (Appender appender : appenders) {
if (new JBossFilterWrapper(appender.getFilter(), true).isLoggable(record)) {
appender.doAppend(event);
outer: for (Appender appender : appenders) {
Filter filter = appender.getFilter();
inner: while (filter != null) {
switch (appender.getFilter().decide(event)) {
case Filter.DENY: {
// skip this appender
continue outer;
}
case Filter.ACCEPT: {
// accept this message
break inner;
}
default: {
// defer decision
break;
}
}
filter = filter.getNext();
}
appender.doAppend(event);
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/apache/log4j/JBossFilterWrapper.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/org/apache/log4j/JBossLogManagerFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static Logger exists(String name) {
*
* @return the logger or {@code null} if no logger is attached.
*/
static Logger getLogger(org.jboss.logmanager.Logger lmLogger) {
public static Logger getLogger(org.jboss.logmanager.Logger lmLogger) {
return getAttachment(lmLogger, LOGGER_KEY);
}

Expand Down
26 changes: 25 additions & 1 deletion src/main/java/org/apache/log4j/spi/LoggingEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.JBossLogManagerFacade;
import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.LogContext;
import org.jboss.modules.Module;

import org.apache.log4j.Category;
Expand Down Expand Up @@ -74,6 +77,7 @@ public class LoggingEvent implements Serializable {
public final transient String fqnOfCategoryClass;
public transient Priority level;

private transient final org.jboss.logmanager.LogContext logContext;
private transient Category logger;

//// Private fields
Expand Down Expand Up @@ -104,6 +108,7 @@ public LoggingEvent(String fqnOfCategoryClass, Category logger, Priority level,
logRecord.setThrown(throwable);
}
logRecord.setMillis(timeStamp = System.currentTimeMillis());
logContext = null;
}

public LoggingEvent(String fqnOfCategoryClass, Category logger, long timeStamp, Priority level, Object message, Throwable throwable) {
Expand All @@ -120,6 +125,7 @@ public LoggingEvent(String fqnOfCategoryClass, Category logger, long timeStamp,
logRecord.setThrown(throwable);
}
logRecord.setMillis(this.timeStamp = timeStamp);
logContext = null;
}

public LoggingEvent(final String fqnOfCategoryClass, final Category logger, final long timeStamp, final Level level, final Object message, final String threadName, final ThrowableInformation throwable, final String ndc, final LocationInfo info, final java.util.Map properties) {
Expand Down Expand Up @@ -157,17 +163,29 @@ public LoggingEvent(final String fqnOfCategoryClass, final Category logger, fina
if (properties != null) {
logRecord.setMdc(properties);
}
logContext = null;
}

// our own constructor
// our own constructors

// This one is for testing only!
public LoggingEvent(final ExtLogRecord logRecord, final Category logger) {
this.logRecord = logRecord;
fqnOfCategoryClass = logRecord.getLoggerClassName();
this.logger = logger;
level = JBossLevelMapping.getPriorityFor(logRecord.getLevel());
categoryName = logRecord.getLoggerName();
timeStamp = logRecord.getMillis();
logContext = null;
}

public LoggingEvent(final ExtLogRecord logRecord, final LogContext logContext) {
this.logRecord = logRecord;
fqnOfCategoryClass = logRecord.getLoggerClassName();
level = JBossLevelMapping.getPriorityFor(logRecord.getLevel());
categoryName = logRecord.getLoggerName();
timeStamp = logRecord.getMillis();
this.logContext = logContext;
}

public LocationInfo getLocationInformation() {
Expand All @@ -186,6 +204,12 @@ public String getLoggerName() {
}

public Category getLogger() {
Category logger = this.logger;
if (logger == null) {
// lazily populate
assert logContext != null; // otherwise, our `logger` would be populated per constructor
logger = this.logger = JBossLogManagerFacade.getLogger(logContext.getLogger(categoryName));
}
return logger;
}

Expand Down
Loading