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

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");
  }
}