Skip to content

Commit

Permalink
Apply the '--log_format_rfc3339' option to the console logger (#308)
Browse files Browse the repository at this point in the history
* Apply the '--log_format_rfc3339' option to the console logger

* [tests] make log level for tests easily configurable + cleanup.

Co-authored-by: Jaime Fullaondo <[email protected]>
  • Loading branch information
hush-hush and truthbk authored Jul 7, 2020
1 parent 10e0610 commit 30bc13e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ matrix:
- jdk: oraclejdk8
env:
- DESC="Oracle JDK8 testing"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties -Dtests.log_level=info"

- jdk: oraclejdk7
env:
- DESC="Oracle JDK7 testing"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties -Dtests.log_level=info"

- jdk: openjdk7
env:
- DESC="OpenJDK7 testing"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties"
- CMD="mvn test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dlog4j.configurationFile=log4j2.travis.properties -Dtests.log_level=info"

script: echo "Running $DESC..." && (eval $CMD)

Expand Down
3 changes: 2 additions & 1 deletion log4j2.travis.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ appender.console.name = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = info
appender.console.filter.threshold.level = all
# appender.console.filter.threshold.level = info
32 changes: 20 additions & 12 deletions src/main/java/org/datadog/jmxfetch/util/CustomLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Filter.Result;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
Expand All @@ -15,9 +15,10 @@
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;



@Slf4j
public class CustomLogger {
private static final Multiset<String> messageCount = HashMultiset.create();
Expand All @@ -38,6 +39,11 @@ public static void setup(Level level, String logLocation, boolean logFormatRfc33

String logPattern = logFormatRfc3339 ? LAYOUT_RFC3339 : LAYOUT;

PatternLayout layout = PatternLayout.newBuilder()
.withConfiguration(config)
.withPattern(logPattern)
.build();

if (logLocation != null
&& !ConsoleAppender.Target.SYSTEM_ERR.toString().equals(logLocation)
&& !SYSTEM_ERR_ALT.equals(logLocation)
Expand All @@ -46,11 +52,6 @@ public static void setup(Level level, String logLocation, boolean logFormatRfc33

target = "FileLogger";

PatternLayout layout = PatternLayout.newBuilder()
.withConfiguration(config)
.withPattern(logPattern)
.build();

RollingFileAppender fa = RollingFileAppender.newBuilder()
.setConfiguration(config)
.withName(target)
Expand All @@ -77,11 +78,6 @@ public static void setup(Level level, String logLocation, boolean logFormatRfc33
config.getRootLogger().removeAppender("CONSOLE");
ctx.updateLoggers();

PatternLayout layout = PatternLayout.newBuilder()
.withConfiguration(config)
.withPattern(logPattern)
.build();

ConsoleAppender ca = ConsoleAppender.newBuilder()
.setConfiguration(config)
.withName(logLocation)
Expand All @@ -95,8 +91,20 @@ public static void setup(Level level, String logLocation, boolean logFormatRfc33
}
}

// replace default appender with the correct layout
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.removeAppender(target);

Appender appender = ConsoleAppender.newBuilder()
.setConfiguration(config)
.withName(target)
.withLayout(layout)
.build();
appender.start();

loggerConfig.addAppender(appender, null, null);
loggerConfig.setLevel(level);

ctx.updateLoggers();
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public class TestCommon {
/** Setup logger. */
@BeforeClass
public static void init() throws Exception {
CustomLogger.setup(Level.toLevel("ALL"), "/tmp/jmxfetch_test.log", false);
String level = System.getProperty("tests.log_level");
if (level == null) {
level = "ALL";
}
CustomLogger.setup(Level.toLevel(level), "/tmp/jmxfetch_test.log", false);
}

/**
Expand Down

0 comments on commit 30bc13e

Please sign in to comment.