Skip to content

Commit

Permalink
Use slf4j+java.util.logging for logging needs. (#383)
Browse files Browse the repository at this point in the history
Also bumps SLF4J from 1.7.26 to 1.7.32.

Co-authored-by: truthbk <truthbk@gmail.com>
Co-authored-by: Srdjan Grubor <sgnn7@sgnn7.org>
Co-authored-by: Olivier Vielpeau <olivier.vielpeau@datadoghq.com>
Co-authored-by: Olivier Vielpeau <olivielpeau@users.noreply.github.com>
  • Loading branch information
5 people committed Dec 21, 2021
1 parent 6081b15 commit 5a842de
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 156 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ target/*

# jenv
.java_version
.java-version
30 changes: 5 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
<apache-commons-lang3.version>3.5</apache-commons-lang3.version>
<java-dogstatsd-client.version>2.10.5</java-dogstatsd-client.version>
<jcommander.version>1.35</jcommander.version>
<!-- log4j 2.13+ drops support for Java 7, so stick to 2.12 -->
<log4j.version>2.12.2</log4j.version>
<slf4j.version>1.7.26</slf4j.version>
<slf4j.version>1.7.32</slf4j.version>
<jackson.version>2.12.3</jackson.version>
<snakeyaml.version>1.26</snakeyaml.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
Expand Down Expand Up @@ -167,28 +165,10 @@
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<!-- use same version as the API -->
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.beust.jcommander.ParameterException;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;

import org.datadog.jmxfetch.reporter.Reporter;
import org.datadog.jmxfetch.tasks.TaskMethod;
Expand All @@ -17,6 +15,7 @@
import org.datadog.jmxfetch.util.ByteArraySearcher;
import org.datadog.jmxfetch.util.CustomLogger;
import org.datadog.jmxfetch.util.FileHelper;
import org.datadog.jmxfetch.util.LogLevel;
import org.datadog.jmxfetch.util.MetadataHelper;
import org.datadog.jmxfetch.util.ServiceCheckHelper;
import org.slf4j.Marker;
Expand Down Expand Up @@ -144,7 +143,7 @@ public static void main(String[] args) {
// not needed in dd-java-agent, which calls run directly.

// Set up the logger to add file handler
CustomLogger.setup(Level.toLevel(config.getLogLevel()),
CustomLogger.setup(LogLevel.fromString(config.getLogLevel()),
config.getLogLocation(),
config.isLogFormatRfc3339());

Expand All @@ -159,21 +158,18 @@ public static void main(String[] args) {
* System#exit}.
*/
public static int run(AppConfig config) {
Marker fatal = MarkerFactory.getMarker("FATAL");
String action = config.getAction();

// The specified action is unknown
if (!AppConfig.ACTIONS.contains(action)) {
log.error(fatal,
action + " is not in " + AppConfig.ACTIONS + ". Exiting.");
log.error(action + " is not in " + AppConfig.ACTIONS + ". Exiting.");
return 1;
}

if (!action.equals(AppConfig.ACTION_COLLECT)
&& !(config.isConsoleReporter() || config.isJsonReporter())) {
// The "list_*" actions can not be used with the statsd reporter
log.error(fatal,
action
log.error(action
+ " argument can only be used with the console or json reporter. Exiting.");
return 1;
}
Expand Down Expand Up @@ -230,8 +226,8 @@ private static void attachShutdownHook() {
@Override
public void run() {
log.info("JMXFetch is closing");
// Properly close log handlers
LogManager.shutdown();
// make sure log handlers are properly closed
CustomLogger.shutdown();
}
}
);
Expand Down Expand Up @@ -1192,7 +1188,7 @@ private <T> void processCollectionStatus(
+ " metrics.";

instanceStatus = Status.STATUS_WARNING;
CustomLogger.laconic(log, Level.WARN, instanceMessage, 0);
CustomLogger.laconic(log, LogLevel.WARN, instanceMessage, 0);
}

if (numberOfMetrics > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.datadog.jmxfetch.reporter.ConsoleReporter;
import org.datadog.jmxfetch.reporter.JsonReporter;
import org.datadog.jmxfetch.reporter.Reporter;
import org.datadog.jmxfetch.validator.Log4JLevelValidator;
import org.datadog.jmxfetch.validator.LogLevelValidator;
import org.datadog.jmxfetch.validator.PositiveIntegerValidator;
import org.datadog.jmxfetch.validator.ReporterValidator;

Expand Down Expand Up @@ -72,7 +72,7 @@ public class AppConfig {
@Parameter(
names = {"--log_level", "-L"},
description = "Level of verbosity",
validateWith = Log4JLevelValidator.class,
validateWith = LogLevelValidator.class,
required = false)
@Builder.Default
private String logLevel = "INFO";
Expand Down
Loading

0 comments on commit 5a842de

Please sign in to comment.