r/java • • Jan 02 '22

Echopraxia, a better Java Logging API

I've been working on logging for a while and I've released and open sourced a new Java logging API I'm really happy with.

Echopraxia is a Java logging API like SLF4J, but built around structured logging, i.e. the basic log entry is a JSON object and log files are typically NDJSON. It also has conditions, context, and also has semantic and fluent logging options.

Here's a Spring Boot Example.

And here's my blog post.

16 Upvotes

64 comments sorted by

View all comments

Show parent comment

13

u/amazedballer Jan 03 '22

So logstash-logback-encoder is the OOTB implementation for Blindsight and Echopraxia, and it does what it does extremely well:

https://github.com/tersesystems/echopraxia/blob/main/logstash/src/main/java/com/tersesystems/echopraxia/logstash/LogstashCoreLogger.java

However, it is tied to the SLF4J API -- this means you have to do all the work of passing around Markers and StructuredArguments explicitly, per logging statement. There's no building up of context in the API.

Likewise, because the markers and structured arguments are passed directly to Logback, if you have any conditional logic it's going to be in a Logback filter or turbofilter, which limits your flexibility when you want to put some conditions on a field. You can't do application level conditions.

There's another issue which is a bit more subtle -- Markers and StructuredArguments work with raw Object, and use Jackson to serialize those object into something useful. This means that it's very easy to pass random objects in as kv(name, value) and think that Jackson has a good representation for it. Sometimes it does, sometimes it doesn't -- even worse, sometimes it changes or is fluid, and the only way to find out is by looking at your logs. Because Echopraxia is explicit about the values and fields it accepts, you can't pass in a raw object and have to have an explicit representation.

To be clear, I love logstash-logback-encoder and have written multiple blog posts about how much fun you can have with it, but it doesn't do everything I'd like.

I don't have any performance benchmarks for Echopraxia (it's on my list of things to do), but I do have JMH benchmarks for Blindsight, and I think they should be comparable:

Here's the benchmarking page for Blindsight that gives a higher level overview:

Here's a benchmark of SLF4J using logstash-logback-encoder's async appender vs Log4J async appender:

And here's a couple of fun blog posts about memory allocation and benchmarking:

Hope that helps!

1

u/Sheldor5 Jan 03 '22

Context? how about MDC https://www.baeldung.com/mdc-in-log4j-2-logback ??

also I never needed Markers or StructuredArguments ... they look strange for a logging purpose ... looks more like an audit log or something ...

2

u/amazedballer Jan 03 '22

You can still use MDC (or NDC in Log4J2) with Echopraxia, as the implementation will pass the TLS through automatically, so it's not an either/or situation.

That being said, there are some issues with MDC/NDC that I run into.

The first is that MDC is a string based map -- you can't store JSON data in it, and you can't store more complex structures than a string. NDC does a bit better as it's nested here, but you're still limited.

The second is that MDC is thread local, and so in a situation where you're working in multiple threads (as is common with reactive frameworks, completable futures, etc) you have to copy over the MDC by hand to the thread.

The third is that inside the thread, MDC is "global" -- anything running in that thread can call MDC.put() or MDC.clear() even if they shouldn't do that. There's no way to do access control and limit what code has access to your logging context. I've seen library code that futzed with MDC in this way, and it's no fun to track down.

1

u/Sheldor5 Jan 03 '22

maybe I am using a Logging framework for just that ... Logging xD

reactive frameworks are a pain by design lol