Skip to content

Commit 824bab9

Browse files
authored
SOLR-18107: fix Log4j2Watcher, register parents (#4113)
The /admin/info/logging endpoint (or just a test) could yield partial logging hierarchies after log4j was upgraded. It should now be robust.
1 parent 4ded44f commit 824bab9

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
2+
title: The /admin/info/logging endpoint (or just a tests) could yield partial logging hierarchies after log4j was upgraded. It should now be robust.
3+
type: fixed # added, changed, fixed, deprecated, removed, dependency_update, security, other
4+
authors:
5+
- name: David Smiley
6+
links:
7+
- name: SOLR-18107
8+
url: https://issues.apache.org/jira/browse/SOLR-18107

solr/core/src/java/org/apache/solr/logging/log4j2/Log4j2Watcher.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ public Log4j2Info(String name, Level level, boolean isSet) {
9090

9191
private final boolean isSet;
9292

93-
@Override
94-
public String getLevel() {
95-
return (level != null) ? level : null;
96-
}
97-
98-
@Override
99-
public String getName() {
100-
return name;
101-
}
102-
10393
@Override
10494
public boolean isSet() {
10595
return isSet;
@@ -199,12 +189,15 @@ public Collection<LoggerInfo> getAllLoggers() {
199189
continue;
200190
}
201191

202-
// NOTE: just because we have an explicit configuration, doesn't mean we have an explitly set
203-
// level
204-
// (Configuration might be for some other property, and level is still inherited)
192+
// NOTE: just because we have an explicit configuration, doesn't mean we have an explicitly
193+
// set level (Configuration might be for some other property, and level is still inherited)
205194
map.putIfAbsent(
206195
name,
207196
new Log4j2Info(name, logger.getLevel(), null != config.getValue().getExplicitLevel()));
197+
198+
// Also add parent loggers in the hierarchy, as they are relevant for level inheritance
199+
// This ensures parent loggers appear even if not in ctx.getLoggers()
200+
addParentLoggers(name, map, ctx);
208201
}
209202

210203
// Now add any "in use" loggers (that aren't already explicitly configured) and their parents
@@ -218,18 +211,35 @@ public Collection<LoggerInfo> getAllLoggers() {
218211
// If we didn't already see a LoggerConfig for these loggers, then their level is
219212
// not (explicitly) set
220213
map.putIfAbsent(name, new Log4j2Info(name, logger.getLevel(), false));
221-
while (true) {
222-
int dot = name.lastIndexOf('.');
223-
if (dot < 0) break;
224-
225-
name = name.substring(0, dot);
226-
map.putIfAbsent(name, new Log4j2Info(name, logger.getLevel(), false));
227-
}
214+
addParentLoggers(name, map, ctx);
228215
}
229216

230217
return map.values();
231218
}
232219

220+
/**
221+
* Adds all parent loggers in the hierarchy for the given logger name.
222+
*
223+
* <p>For example, given "org.apache.solr.core.SolrCore", this will add:
224+
*
225+
* <ul>
226+
* <li>"org.apache.solr.core"
227+
* <li>"org.apache.solr"
228+
* <li>"org"
229+
* </ul>
230+
*/
231+
private void addParentLoggers(String loggerName, Map<String, LoggerInfo> map, LoggerContext ctx) {
232+
String parentName = loggerName;
233+
while (true) {
234+
int dot = parentName.lastIndexOf('.');
235+
if (dot < 0) break;
236+
237+
parentName = parentName.substring(0, dot);
238+
map.putIfAbsent(
239+
parentName, new Log4j2Info(parentName, ctx.getLogger(parentName).getLevel(), false));
240+
}
241+
}
242+
233243
@Override
234244
public void setThreshold(String level) {
235245
Log4j2Appender app = getAppender();

0 commit comments

Comments
 (0)