Skip to content

Commit

Permalink
[log4j] fixing file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
truthbk committed Jan 10, 2020
1 parent 77d405a commit c45f91c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/java/org/datadog/jmxfetch/util/CustomLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,18 +41,18 @@ 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")
.withPolicy(SizeBasedTriggeringPolicy.createPolicy("5MB"))
.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 {

Expand All @@ -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();
}

Expand Down

0 comments on commit c45f91c

Please sign in to comment.