Skip to content

Commit 7c16c6c

Browse files
committed
Use empty string for null logging prefix
When a logging prefix is null (e.g., when we do not yet know the node name), this commit causes such loggers to use an empty string marker instead of an empty prefix (i.e., "[] ") marker.
1 parent f9112bf commit 7c16c6c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

core/src/main/java/org/elasticsearch/common/logging/PrefixLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String prefix() {
4444
super(logger, name, null);
4545
this.prefix = prefix;
4646

47-
final String actualPrefix = (prefix == null ? "[] " : prefix).intern();
47+
final String actualPrefix = (prefix == null ? "" : prefix).intern();
4848
final Marker actualMarker;
4949
synchronized (markers) {
5050
final WeakReference<Marker> marker = markers.get(actualPrefix);

qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void testFindAppender() throws IOException {
114114
public void testPrefixLogger() throws IOException, IllegalAccessException {
115115
setupLogging("prefix");
116116

117-
final String prefix = randomAsciiOfLength(16);
117+
final String prefix = randomBoolean() ? null : randomAsciiOfLength(16);
118118
final Logger logger = Loggers.getLogger("prefix", prefix);
119119
logger.info("test");
120120
logger.info("{}", "test");
@@ -131,7 +131,11 @@ public void testPrefixLogger() throws IOException, IllegalAccessException {
131131
final int expectedLogLines = 3;
132132
assertThat(events.size(), equalTo(expectedLogLines + stackTraceLength));
133133
for (int i = 0; i < expectedLogLines; i++) {
134-
assertThat(events.get(i), startsWith("[" + prefix + "]"));
134+
if (prefix == null) {
135+
assertThat(events.get(i), startsWith("test"));
136+
} else {
137+
assertThat(events.get(i), startsWith("[" + prefix + "] test"));
138+
}
135139
}
136140
}
137141

0 commit comments

Comments
 (0)