-
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
Implement custom JUL bridge #96872
Changes from 4 commits
430fe31
871354a
7a316a5
a79079b
adf8717
b3f4258
a75db21
e8900c0
5249d93
9a74288
2fb8bba
a5d582f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.Logger; | ||
import org.elasticsearch.logging.LogManager; | ||
|
||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.TreeMap; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogRecord; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A Java Util Logging handler that writes log messages to log4j. | ||
*/ | ||
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. should be safe. the getHandlers return a copied array of handlers |
||
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(); | ||
if (thrown == null) { | ||
logger.log(level, record.getMessage()); | ||
|
||
} else { | ||
logger.log(level, record::getMessage, thrown); | ||
|
||
} | ||
} | ||
|
||
private Level translateJulLevel(java.util.logging.Level julLevel) { | ||
Level log4jLevel = levelMap.get(julLevel); | ||
|
||
if (log4jLevel != null) { | ||
return log4jLevel; | ||
} | ||
// 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() {} | ||
} |
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.
This should be EsLogger instead log4j