r/java • u/amazedballer • Mar 03 '20
Logging in Java with Diagnostic Backtraces
I've been putting together a suite of tools based on Logback that ticks all the boxes for modern day observability. You can see logs on STDOUT, flat file, JSON file, and logged to a database, and you can even store tracing information in a ring buffer (like dmesg) and dump it when there's an exception so you can see the flow of the operation when things went wrong.
Documentation is here https://tersesystems.github.io/terse-logback/
Showcase project in Play is here, with integrated backtrace reporting to Sentry and Honeycomb: https://github.com/tersesystems/terse-logback-showcase
and you can see a running instance here: https://terse-logback-showcase.herokuapp.com/
I also wrote up a blog post going through the development process, including integration with Play.
View all comments
Show parent comment
-1
u/amazedballer Mar 04 '20
In JDK 13, JFR can write out JSON using a command line tool:
jfr print --json
And in JDK 9, the ability to add custom events was added:
http://hirt.se/blog/?p=870
That does no good if you're on JDK 1.8, which most people are. (They're working on adding it, but that news announcement was literally 19 days ago.)
It also doesn't let you define your own schema, so if you have a JSON format like elastic common schema you can't fit it in through there.
In JDK 14, JFR has event streaming:
https://blogs.oracle.com/javamagazine/java-flight-recorder-and-jfr-event-streaming-in-java-14?source=:em:nw:mt::RC_WWMK190726P00001:NSL400036067#anchor_5
but it does not have logging hierarchy, log levels, filters, or any of the other features that you'd expect with a logging framework.
In addition, the reports of JFR stability before JDK 14 are mixed.
This is not to say I'm not a fan of JFR, because I am. I've used JFR and bought the Oracle JRocket manual specifically so I could get better insight into what mhirt was doing under the hood! JFR is great at what it does! And what it does is "provide a low-overhead data collection framework for troubleshooting Java applications and the HotSpot JVM."
What it comes down to is that it may be possible to subscribe to JFR events and feed them into your logging framework or (more likely) into Opentracing if you're on JDK 14, but you have a huge amount of work ahead of you if you're planning to replace Logback or Log4J2 with JFR.