Skip to content
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

Change default Java Util Logging format #96519

Closed
wants to merge 5 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ static List<String> systemJvmOptions() {
* explore alternatives. See org.elasticsearch.xpack.searchablesnapshots.preallocate.Preallocate.
*/
"--add-opens=java.base/java.io=org.elasticsearch.preallocate",
javaUtilLoggingDefaultFormat(),
maybeOverrideDockerCgroup()
).stream().filter(e -> e.isEmpty() == false).collect(Collectors.toList());
}

// scope for testing
static String javaUtilLoggingDefaultFormat() {
return "-Djava.util.logging.SimpleFormatter.format=[%4$s][%3$s] %5$s%6$s%n";
}

/*
* The virtual file /proc/self/cgroup should list the current cgroup
* membership. For each hierarchy, you can follow the cgroup path from
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.server.cli;

import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.test.ESTestCase;
import org.junit.BeforeClass;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Logger;

import static org.hamcrest.Matchers.equalTo;

public class JavaUtilLoggingFormatTests extends ESTestCase {
@BeforeClass
public static void init() {
assert "false".equals(System.getProperty("tests.security.manager")) : "-Dtests.security.manager=false has to be set";
}

@SuppressForbidden(reason = "testing the behaviour of 3rd party using jul")
public void testJavaUtilLoggingFormat() {
String key = key(SystemJvmOptions.javaUtilLoggingDefaultFormat());
String value = value(SystemJvmOptions.javaUtilLoggingDefaultFormat());
System.setProperty(key, value);

final ByteArrayOutputStream myOut = new ByteArrayOutputStream();
PrintStream err = System.err;
PrintStream out = System.out;
try {
System.setErr(new PrintStream(myOut));
System.setOut(new PrintStream(myOut));

Logger.getLogger("any").info("message");

assertThat(myOut.toString(), equalTo("[INFO][any] message\n"));
} finally {
System.setOut(out);
System.setErr(err);
System.getProperties().remove(key);
}
}

public String key(String s) {
return s.split("=")[0].substring(2);
}

public String value(String s) {
return s.split("=")[1];
}
}