Skip to content

Commit

Permalink
[MODULES-447] Make the ModuleLoggerFinder.activate() method public. A…
Browse files Browse the repository at this point in the history
…lso, activate the ModuleLoggerFinder if a threshold is hit.

https://issues.redhat.com/browse/MODULES-447
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Feb 23, 2024
1 parent 3dde921 commit 611274d
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 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 @@ -87,7 +89,7 @@ public System.Logger getLogger(final String name, final java.lang.Module module)
*
* @param cl the class loader to use
*/
static void activate(final ClassLoader cl) {
public static void activate(final ClassLoader cl) {
// To keep order, we must observe the lock
lock.lock();
try {
Expand Down Expand Up @@ -142,6 +144,24 @@ static void activate(final ClassLoader cl) {
}
}

private static int getMaxLogMessages() {
final int dft = 10_000;
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

0 comments on commit 611274d

Please sign in to comment.