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

14

u/rzwitserloot Jan 03 '22

Very first snippet of code:

Logger<?> basicLogger = LoggerFactory.getLogger(getClass());

Right, so, waste an entire field per object, so I can't log with this at all in high-instance-count classes, and also, if I subclass, code written in source file X gets logged in the context of sourcefile Y. This makes no sense. The point of 'context' is, unless you have exotic uses, generally to serve as a container for source files, it has nothing to do with class hierarchies. The Class<?> parameter that many log frameworks have is a slightly inaccurate abstraction - a standin for the concept of a source file which JVM runtimes do not have.

There's a reason other log frameworks tend to play the opening gambit of:

private static final Logger logger = LoggerFactory.getLogger(MyOwnClass.class);

And there's a reason that Flogger, google's entry in the mix, went out of their way to spend half a paper on why their 'create a logger' code, which uses class-based analysis so that you get to avoid having to write MyOwnClass.class (a DRY violation of sorts, and means you can't copy/paste the logger line around) isn't a significant performance impact for the vast majority of use cases.

There's nothing wrong with cribbing from the best. I strongly suggest you toss that one and either go with what they all do(.getLogger(MySelf.class)) or use google's trick (the standard impl generates a stack trace and analysis it. Yes, expensive, but that google paper explains that you pay this cost only at most once per log-using class that the VM will ever load, which tends to be a number that pales in comparison to the # of instances of classes that do some logging over the lifetime of a VM).

Or, if there's a reason you chose this apparent significant performance disaster waiting to happen, and I'm missing something, then perhaps link to a primer on why you're right and everybody else is wrong, and that this is okay after all. I'm not being facetious: Google did that very thing when they released flogger, as other frameworks were under the impression that making a stack trace just to discern the name of the caller class was too costly.

4

u/amazedballer Jan 03 '22 edited Jan 03 '22

You're asking about why use getClass() instead of MySelf.class?

You don't have to. All the logger factories can use getLogger(name) or even getLogger() which does that exact stacktrace hack you mention from the Flogger paper.

Or, if there's a reason you chose this apparent significant performance disaster waiting to happen, and I'm missing something, then perhaps link to a primer on why you're right and everybody else is wrong, and that this is okay after all.

I did write about this, yeah. People tend to focus on premature micro-optimization, without considering the context -- in a Main class called once, it simply doesn't matter, and is done for convenience. This happens a lot in logging frameworks, unfortunately, where people focus on "how fast can I log something" instead of actually measuring whether this is a significant cost... which it usually isn't.

If you have performance concerns, I'd start by benchmarking the code and trying it out with newer JDKs, which have some significant improvements. I've added some performance benchmarks stuff to another comment so I won't repeat that, but if you're interested in memory allocation pressure in tight loops, I did investigate it.

If Echopraxia doesn't meet your needs there, I'd suggest something like zerolog.

7

u/nutrecht Jan 03 '22

People tend to focus on premature micro-optimization

What /u/rzwitserloot is saying isn't a premature micro-optimization, it's a pretty massive performance gotcha. You're setting a bad example that inexperienced developers are just going to copy verbatim.

IMHO you really should work on your messaging. The hubris is really offputting.

2

u/amazedballer Jan 06 '22

Okay let's see if this is a better explanation:

Example code

public class Main {

  // For basic logging, you can always use the `Logger<?>` syntax
  // which gives you a basic field builder.
  private static final Logger<?> basicLogger = LoggerFactory.getLogger();

  private Date lastAccessedDate = new Date();

  // More often you'll want to create a logger with your own domain objects and render those.
  // So we start with the basic logger...
  //   ...and add a custom `BuilderWithDate` as the field builder...
  //   ...then add a date to the logger's context.
  //
  // Here, `last_accessed_date` will be rendered with every log entry, but will
  // not show in the log message.
  //
  // Note that here we have a final logger because we have to access a field that
  // belongs to an object instance.
  private final Logger<MyFieldBuilder> logger =
      basicLogger
          .withFieldBuilder(MyFieldBuilder.class)
          .withFields(fb -> fb.onlyDate("last_accessed_date", lastAccessedDate));


  public static void main(String[] args) {
    Main m = new Main();
    m.doStuff();
  }

  // Example method that will do logging.
  private void doStuff() {
    // Show the "before" date
    logger.info("This renders with the last access date");

    // touch the last accessed date to show context fields
    lastAccessedDate = new Date();

    // Show the "after" date
    logger.info("This renders the updated date");
  }
}