Skip to content

Commit

Permalink
Configurable LOG LEVEL #55 (#56)
Browse files Browse the repository at this point in the history
* master - change loglevel
  • Loading branch information
DanielFidalgo authored and yyyogev committed Sep 11, 2019
1 parent f71aa80 commit c53f458
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ This lib can also get the metrics from MBean Platform instead of jolokia. In ord
- The parameters names and functions are exactly as described in Environment Variables section. (Except no need to specify JOLOKIA_URL of course)
- The javaagent.jar is an "Uber-Jar" that shades all of its dependencies inside, to prevent class collisions
- For example: java -javaagent:jmx2graphite.jar=GRAPHITE_HOSTNAME=graphite.example.com,SERVICE_NAME=PROD.MyAwesomeCategory example.jar

- To configure Log Level add the configuration LOG_LEVEL=[any of OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL]



# How to expose JMX Metrics using Jolokia Agent
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.logz</groupId>
<artifactId>jmx2graphite</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>

<build>
<plugins>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/logz/jmx2graphite/Jmx2Graphite.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.logz.jmx2graphite;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -41,6 +46,8 @@ public Jmx2Graphite(Jmx2GraphiteConfiguration conf) {
else {
throw new IllegalConfiguration("Unsupported client type: " + conf.getMetricClientType());
}

configureLogLevel(conf.getLogLevel());
}

public void run() {
Expand Down Expand Up @@ -93,4 +100,12 @@ public void run() {
}
}
}

private void configureLogLevel (String level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.valueOf(level));
ctx.updateLoggers();
}
}
17 changes: 17 additions & 0 deletions src/main/java/io/logz/jmx2graphite/Jmx2GraphiteConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URL;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

/**
* @author amesika
Expand All @@ -33,6 +34,8 @@ public class Jmx2GraphiteConfiguration {
private MetricClientType metricClientType;
private GraphiteProtocol graphiteProtocol;

private String logLevel;

public GraphiteProtocol getGraphiteProtocol() {
return graphiteProtocol;
}
Expand Down Expand Up @@ -101,6 +104,12 @@ else if (config.hasPath("service.poller.mbean-direct")) {
} else {
graphiteWriteTimeoutMs = Math.round(0.7f * TimeUnit.SECONDS.toMillis(metricsPollingIntervalInSeconds));
}

if(config.hasPath("log.level")) {
logLevel = config.getString("log.level");
} else {
logLevel = Level.INFO.getName();
}
}

private GraphiteProtocol getGraphiteProtocol(Config config) {
Expand Down Expand Up @@ -191,4 +200,12 @@ public int getGraphiteSocketTimeout() {
public int getGraphiteWriteTimeoutMs() {
return graphiteWriteTimeoutMs;
}

public String getLogLevel() {
return logLevel;
}

public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/logz/jmx2graphite/Jmx2GraphiteJavaAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ private static String getArgumentConfigurationRepresentation(String key) throws
return "graphite.writeTimeout";
case "GRAPHITE_PROTOCOL":
return "graphite.protocol";
case "LOG_LEVEL":
return "log.level";
default:
throw new IllegalConfiguration("Unknown configuration option: " + key);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/javaagent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ graphite {
protocol = ${?GRAPHITE_PROTOCOL}
}

metricsPollingIntervalInSeconds = ${?INTERVAL_IN_SEC}
metricsPollingIntervalInSeconds = ${?INTERVAL_IN_SEC}

logLevel = ${?LOG_LEVEL}

0 comments on commit c53f458

Please sign in to comment.