Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,11 @@
<sha256 value="47f768ffd66107a66f0c2a19445ab1e42ce6719a7f30f9aa9ef96157c83949fd" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.logging.log4j" name="log4j-jul" version="2.19.0">
<artifact name="log4j-jul-2.19.0.jar">
<sha256 value="c3f0cbd1e455b1f3443c1bf0860fa3a91f5ae721d1acfeff393629fdefc23b6b" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.logging.log4j" name="log4j-slf4j-impl" version="2.19.0">
<artifact name="log4j-slf4j-impl-2.19.0.jar">
<sha256 value="015d5c229f3cd5c0ebf175c1da08d596d94043362ae9d92637d88848c90537c8" origin="Generated by Gradle"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testLoggingLevelsFromSettings() throws IOException, UserException {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Map<String, LoggerConfig> loggerConfigs = config.getLoggers();
assertThat(loggerConfigs.size(), equalTo(3));
assertThat(loggerConfigs.size(), equalTo(5));
assertThat(loggerConfigs, hasKey(""));
assertThat(loggerConfigs.get("").getLevel(), equalTo(rootLevel));
assertThat(loggerConfigs, hasKey("foo"));
Expand Down
1 change: 1 addition & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ tasks.named("thirdPartyAudit").configure {

tasks.named("dependencyLicenses").configure {
mapping from: /lucene-.*/, to: 'lucene'
mapping from: /log4j-.*/, to: 'log4j'
dependencies = project.configurations.runtimeClasspath.fileCollection {
it.group.startsWith('org.elasticsearch') == false ||
// keep the following org.elasticsearch jars in
Expand Down
File renamed without changes.
202 changes: 0 additions & 202 deletions server/licenses/log4j-core-LICENSE.txt

This file was deleted.

20 changes: 0 additions & 20 deletions server/licenses/log4j-core-NOTICE.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
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 TreeMap<Integer, Level> sortedLevelMap = new TreeMap<>(
levelMap.entrySet().stream().collect(
Collectors.toMap(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());
String message = record.getMessage();
Throwable thrown = record.getThrown();
logger.log(level, message, 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() {}
}
Loading