Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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.
Copy link
Contributor

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

*/
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<>(
Copy link
Contributor

@uschindler uschindler Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also write:

 sortedLevelMap = levelMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().intValue(), Map.Entry::getValue,
  (k1, k2) -> throw new UnsupportedOperationException(), TreeMap::new));

(I hate that theres no Collector method that allows to specify custom map factory, but uses default merge function. 🤮

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have Maps.toUnmodifiableSortedMap that collects to a NavigableMap

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I used the Maps method and changed to NavigableMap

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()) {
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still log4j 🤨💪

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