-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Implement custom JUL bridge #96872
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
Merged
Merged
Implement custom JUL bridge #96872
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
430fe31
Implement custom JUL bridge
rjernst 871354a
fix metadata
rjernst 7a316a5
spotless
rjernst a79079b
iter
rjernst adf8717
fix javadoc and imports
rjernst b3f4258
spotless
rjernst a75db21
Merge branch 'main' into logging/jul2
rjernst e8900c0
add null and formatted message
rjernst 5249d93
spotless
rjernst 9a74288
scrub log4j from impl
rjernst 2fb8bba
Merge branch 'main' into logging/jul2
rjernst a5d582f
create MessageFormat explicitly with locale
rjernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
server/src/main/java/org/elasticsearch/common/logging/JULBridge.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.common.logging; | ||
|
||
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.logging.Level; | ||
import org.elasticsearch.logging.LogManager; | ||
import org.elasticsearch.logging.Logger; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogRecord; | ||
|
||
/** | ||
* A Java Util Logging handler that writes log messages to the Elasticsearch logging framework. | ||
*/ | ||
class JULBridge extends Handler { | ||
|
||
private static final Map<java.util.logging.Level, Level> levelMap = Map.of( | ||
java.util.logging.Level.OFF, | ||
Level.OFF, | ||
java.util.logging.Level.SEVERE, | ||
Level.ERROR, | ||
java.util.logging.Level.WARNING, | ||
Level.WARN, | ||
java.util.logging.Level.INFO, | ||
Level.INFO, | ||
java.util.logging.Level.FINE, | ||
Level.DEBUG, | ||
java.util.logging.Level.FINEST, | ||
Level.TRACE, | ||
java.util.logging.Level.ALL, | ||
Level.ALL | ||
); | ||
|
||
private static final NavigableMap<Integer, Level> sortedLevelMap = levelMap.entrySet() | ||
.stream() | ||
.collect(Maps.toUnmodifiableSortedMap(e -> e.getKey().intValue(), Map.Entry::getValue)); | ||
|
||
public static void install() { | ||
var rootJulLogger = java.util.logging.LogManager.getLogManager().getLogger(""); | ||
// clear out any other handlers, so eg we don't also print to stdout | ||
for (var existingHandler : rootJulLogger.getHandlers()) { | ||
rootJulLogger.removeHandler(existingHandler); | ||
} | ||
rootJulLogger.addHandler(new JULBridge()); | ||
} | ||
|
||
private JULBridge() {} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
Logger logger = LogManager.getLogger(record.getLoggerName()); | ||
Level level = translateJulLevel(record.getLevel()); | ||
Throwable thrown = record.getThrown(); | ||
|
||
String rawMessage = record.getMessage(); | ||
final String message; | ||
if (rawMessage == null) { | ||
message = "<null message>"; | ||
} else { | ||
message = new MessageFormat(rawMessage, Locale.ROOT).format(record.getParameters()); | ||
} | ||
|
||
if (thrown == null) { | ||
logger.log(level, message); | ||
} else { | ||
logger.log(level, () -> message, thrown); | ||
} | ||
} | ||
|
||
private Level translateJulLevel(java.util.logging.Level julLevel) { | ||
Level esLevel = levelMap.get(julLevel); | ||
if (esLevel != null) { | ||
return esLevel; | ||
} | ||
// no matching known level, so find the closest level by int value | ||
var closestEntry = sortedLevelMap.lowerEntry(julLevel.intValue()); | ||
assert closestEntry != null; // not possible since ALL is min int | ||
return closestEntry.getValue(); | ||
} | ||
|
||
@Override | ||
public void flush() {} | ||
|
||
@Override | ||
public void close() {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a good idea to remove elements while executing the loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an array, so it doesn't actually change as remove is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be safe. the getHandlers return a copied array of handlers