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

[MODULES-447] Make the ModuleLoggerFinder.activate() method public. A… #328

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/main/java/org/jboss/modules/ModuleLoggerFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public System.Logger getLogger(final String name, final java.lang.Module module)
}
};

private static final int MAX_LOG_MESSAGES = getMaxLogMessages();

private static final Map<String, System.Logger> loggers = new ConcurrentHashMap<>();
private static final ReentrantLock lock = new ReentrantLock();
private static final Deque<SystemLogRecord> messages = new LinkedBlockingDeque<>();
Expand Down Expand Up @@ -142,6 +144,24 @@ static void activate(final ClassLoader cl) {
}
}

private static int getMaxLogMessages() {
final int dft = 10_000;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note this is a completely arbitrary number and if there is a better default, please let me know.

final String value;
if (System.getSecurityManager() == null) {
value = System.getProperty("jboss.modules.logger.finder.threshold");
} else {
value = AccessController.doPrivileged(new PropertyReadAction("jboss.modules.logger.finder.threshold"));
}
if (value == null) {
return dft;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException ignore) {
return dft;
}
}

private static class DelegatingSystemLogger implements System.Logger {

private final AtomicReference<System.Logger> delegate;
Expand Down Expand Up @@ -289,12 +309,33 @@ public boolean isLoggable(final System.Logger.Level level) {

@Override
public void log(final System.Logger.Level level, final ResourceBundle bundle, final String msg, final Throwable thrown) {
messages.addLast(new SystemLogRecord(name, module, level, bundle, msg, null, thrown));
if (messages.size() >= MAX_LOG_MESSAGES) {
activate(getClassLoader());
}
if (activated) {
finder.getLogger(name, module).log(level, bundle, msg, thrown);
} else {
messages.addLast(new SystemLogRecord(name, module, level, bundle, msg, null, thrown));
}
}

@Override
public void log(final System.Logger.Level level, final ResourceBundle bundle, final String format, final Object... params) {
messages.addLast(new SystemLogRecord(name, module, level, bundle, format, params, null));
if (messages.size() >= MAX_LOG_MESSAGES) {
activate(getClassLoader());
}
if (activated) {
finder.getLogger(name, module).log(level, bundle, format, params);
} else {
messages.addLast(new SystemLogRecord(name, module, level, bundle, format, params, null));
}
}

private ClassLoader getClassLoader() {
if (System.getSecurityManager() == null) {
return getClass().getClassLoader();
}
return AccessController.doPrivileged(((PrivilegedAction<ClassLoader>) () -> getClass().getClassLoader()));
}
}

Expand Down