ramanathan1504 commented on code in PR #4465:
URL: https://github.com/apache/logging-log4j2/pull/4465#discussion_r4505311577
##########
log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java:
##########
@@ -125,22 +131,34 @@ protected void doSetLevel(final Level newLevel) throws
SecurityException {
@Override
public void setUseParentHandlers(boolean useParentHandlers) {
- LOGGER.warn(MUTATOR_DISABLED, "setUseParentHandlers",
useParentHandlers);
+ warnMutatorAccess("setUseParentHandlers", useParentHandlers);
super.setUseParentHandlers(useParentHandlers);
}
@Override
public void addHandler(Handler handler) throws SecurityException {
- LOGGER.warn(MUTATOR_DISABLED, "addHandler", handler);
+ warnMutatorAccess("addHandler", handler);
super.addHandler(handler);
}
@Override
public void removeHandler(Handler handler) throws SecurityException {
- LOGGER.warn(MUTATOR_DISABLED, "removeHandler", handler);
+ warnMutatorAccess("removeHandler", handler);
super.removeHandler(handler);
}
+ private static void warnMutatorAccess(final String method, final Object
argument) {
+ final String mode =
PropertiesUtil.getProperties().getStringProperty("log4j2.julMutatorAccess",
"warn-once");
+ if ("ignore".equalsIgnoreCase(mode) || !LOGGER.isWarnEnabled()) {
+ return;
+ }
+ if (!"warn".equalsIgnoreCase(mode) &&
!MUTATOR_WARNING_REPORTED.compareAndSet(false, true)) {
+ return;
+ }
+ final StackTraceElement caller = StackLocatorUtil.calcLocation(FQCN);
+ LOGGER.warn(MUTATOR_DISABLED, method, argument, caller == null ?
"unknown" : caller.getClassName());
Review Comment:
`calcLocation(FQCN)` only returns null on Java 8 with an empty stack trace,
and no test reaches `"unknown"`. Passing the `StackTraceElement` itself drops
the branch. The message then shows the class, method and line, or `null`.
```suggestion
LOGGER.warn(MUTATOR_DISABLED, method, argument,
StackLocatorUtil.calcLocation(FQCN));
```
##########
src/changelog/.2.x.x/3281_jul_mutator_warnings.xml:
##########
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="changed">
+ <issue id="3281"
link="https://github.com/apache/logging-log4j2/issues/3281"/>
+ <description format="asciidoc">
+ Limit JUL mutator warnings to one per class loader by default, identify
the caller,
+ and add `log4j2.julMutatorAccess` to configure warning frequency.
Review Comment:
Changelog entries are one short line. Can this be cut to the one below?
```suggestion
Warn once about JUL mutator calls and name the calling class.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]