Skip to content

Commit

Permalink
Merge pull request #512 from DataDog/sopell/millisecond-logs
Browse files Browse the repository at this point in the history
Adds option to emit logs with millisecond timestamp

Co-authored-by: scottopell <[email protected]>
  • Loading branch information
dd-mergequeue[bot] and scottopell authored Jan 25, 2024
2 parents 9482fbf + 59d60d7 commit d70a4a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ If you are an sdkman user, there is a config file present in this project with
the recommended JDK version for development, use `sdk env` to activate it.


### Enabling file line numbers in log messages
If you set the system property `-Djmxfetch.filelinelogging=true`, this will enable all log output to
include the line number which emitted a given log.
### Logging Options
The logging output is configured in code in `CustomLogger.java`.
The following system properties will affect the format of the log records.

- `-Djmxfetch.filelinelogging=true`
- All log records will include the specific file and line number which emitted
that log.
- `-Djmxfetch.millisecondlogging=true`
- All log records will include milliseconds in the timestamp, rather than the default
'second' timestamp resolution.

### Local Testing
You can utilize the provided testing server `misbehaving-jmx-server` with the
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/datadog/jmxfetch/util/CustomLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public class CustomLogger {

// Enable by setting -Djmxfetch.filelinelogging,
// if true, log record will include the source file and line number
private static boolean enableFileLineLogging =
private static boolean enableFileLineLogging =
System.getProperty("jmxfetch.filelinelogging", "false").equals("true");

// Enable by setting -Djmxfetch.millisecondLogging,
// if true, log record will include milliseconds
private static boolean millisecondLogging =
System.getProperty("jmxfetch.millisecondLogging", "false").equals("true");

private static final ConcurrentHashMap<String, AtomicInteger> messageCount
= new ConcurrentHashMap<String, AtomicInteger>();

Expand Down Expand Up @@ -63,7 +68,10 @@ private static boolean isStdOut(String target) {
public static synchronized void setup(LogLevel level, String logLocation,
boolean logFormatRfc3339) {
String target = "CONSOLE";
final String dateFormat = logFormatRfc3339 ? DATE_JDK14_LAYOUT_RFC3339 : DATE_JDK14_LAYOUT;
String dateFormat = logFormatRfc3339 ? DATE_JDK14_LAYOUT_RFC3339 : DATE_JDK14_LAYOUT;
if (millisecondLogging) {
dateFormat = dateFormat.replace("ss", "ss.SSS");
}
final SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat,
Locale.getDefault());

Expand Down

0 comments on commit d70a4a4

Please sign in to comment.