From 0d9c91ab2410f3bd8d2ba3e7a9384f1cd68abfe0 Mon Sep 17 00:00:00 2001 From: Carlos Roman Date: Wed, 5 Jun 2024 15:53:41 +0100 Subject: [PATCH] Making thread name in logs the default --- .../java/org/datadog/jmxfetch/AppConfig.java | 2 +- .../org/datadog/jmxfetch/util/CustomLogger.java | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index 5f736d8a..db63e369 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -101,7 +101,7 @@ public class AppConfig { description = "Logs the thread name with each message", required = false) @Builder.Default - private boolean logThreadName = false; + private boolean logThreadName = true; @Parameter( names = {"--conf_directory", "-D"}, diff --git a/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java b/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java index 5b462814..131c88e3 100644 --- a/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java +++ b/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java @@ -139,8 +139,6 @@ private static SimpleFormatter getFormatter( // log format return new SimpleFormatter() { - private final String formatLayout = - logThreadName ? JDK14_WITH_THREADS_LAYOUT : JDK14_LAYOUT; private String simpleClassName(String str) { int start = str.lastIndexOf('.'); @@ -170,14 +168,8 @@ public synchronized String format(LogRecord lr) { Throwable throwable = new Throwable(); StackTraceElement logEmissionFrame = throwable.getStackTrace()[6]; - final String logFormat; if (logThreadName) { - logFormat = JDK14_WITH_THREADS_LAYOUT_FILE_LINE; - } else { - logFormat = JDK14_LAYOUT_FILE_LINE; - } - if (logThreadName) { - return String.format(logFormat, + return String.format(JDK14_WITH_THREADS_LAYOUT_FILE_LINE, dateFormatter.format(new Date()), LogLevel.fromJulLevel(lr.getLevel()).toString(), Thread.currentThread().getName(), @@ -187,7 +179,8 @@ public synchronized String format(LogRecord lr) { exception ); } - return String.format(logFormat, + + return String.format(JDK14_LAYOUT_FILE_LINE, dateFormatter.format(new Date()), LogLevel.fromJulLevel(lr.getLevel()).toString(), logEmissionFrame.getFileName(), @@ -198,7 +191,7 @@ public synchronized String format(LogRecord lr) { } if (logThreadName) { - return String.format(formatLayout, + return String.format(JDK14_WITH_THREADS_LAYOUT, dateFormatter.format(new Date()), LogLevel.fromJulLevel(lr.getLevel()).toString(), Thread.currentThread().getName(), @@ -207,7 +200,7 @@ public synchronized String format(LogRecord lr) { exception ); } - return String.format(formatLayout, + return String.format(JDK14_LAYOUT, dateFormatter.format(new Date()), LogLevel.fromJulLevel(lr.getLevel()).toString(), simpleClassName(lr.getSourceClassName()),