From c45f91ca8389b2e51266e30be2589716f548122e Mon Sep 17 00:00:00 2001 From: Jaime Fullaondo Date: Fri, 10 Jan 2020 14:40:43 +0100 Subject: [PATCH] [log4j] fixing file logger --- .../org/datadog/jmxfetch/util/CustomLogger.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java b/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java index 6a468ccbb..246acbc26 100644 --- a/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java +++ b/src/main/java/org/datadog/jmxfetch/util/CustomLogger.java @@ -26,11 +26,13 @@ public class CustomLogger { public static void setup(Level level, String logLocation) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); + String target = "CONSOLE"; if (logLocation != null && !ConsoleAppender.Target.SYSTEM_ERR.equals(logLocation) && !ConsoleAppender.Target.SYSTEM_OUT.equals(logLocation)) { + target = "FileLogger"; PatternLayout layout = PatternLayout.newBuilder() .withConfiguration(config) @@ -39,7 +41,7 @@ public static void setup(Level level, String logLocation) { RollingFileAppender fa = RollingFileAppender.newBuilder() .setConfiguration(config) - .withName("FileLogger") + .withName(target) .withLayout(layout) .withFileName(logLocation) .withFilePattern(logLocation + ".%d") @@ -47,10 +49,10 @@ public static void setup(Level level, String logLocation) { .withStrategy(DefaultRolloverStrategy.newBuilder().withMax("1").build()) .build(); + fa.start(); config.addAppender(fa); - Configurator.setLevel("FileLogger", level); + ctx.getRootLogger().addAppender(config.getAppender(fa.getName())); - fa.start(); log.info("File Handler set"); } else { @@ -69,13 +71,13 @@ public static void setup(Level level, String logLocation) { .withLayout(layout) .build(); - config.addAppender(ca); ca.start(); + config.addAppender(ca); + ctx.getRootLogger().addAppender(config.getAppender(ca.getName())); } - - Configurator.setLevel("CONSOLE", level); } + Configurator.setLevel(target, level); ctx.updateLoggers(); }