r/java • u/darenkster • Oct 30 '23
POC: String Template Processors for SLF4J
JDK 21 introduced String Templates with JEP430.
I was wondering what you could do with it and came up with a proof of concept for processors as a façade for SLF4J.
In a class LOG I created the processors as static fields for each log level. These can be statically imported and called like this:
import static de.darenkster.stringtemplates2slf4j.loggers.LOG.*;
...
var test = "log";
var ex = new Exception();
INFO."This is a info \{test}";
ERROR."This is a error \{test} \{ex}";
WARN."This is a debug \{test}";
TRACE."This is a debug \{test}";
This would result in the following log output:
10:33:44.899 [main] INFO de.darenkster.stringtemplates2slf4j.Main -- This is a info log
10:33:44.907 [main] ERROR de.darenkster.stringtemplates2slf4j.Main -- This is a error log java.lang.Exception
java.lang.Exception: null
at de.darenkster.stringtemplates2slf4j.Main.main(Main.java:9)
10:33:44.909 [main] DEBUG de.darenkster.stringtemplates2slf4j.Main -- This is a debug log
10:33:44.910 [main] WARN de.darenkster.stringtemplates2slf4j.Main -- This is a warn log
The calling class is determined via the StackWalker API and depending on whether the String Templates contains an exception the corresponding log method with the throwable parameter ist called. Here's the link to the GitHUb Repo if you want to check it out: https://github.com/darenkster/LoggingWithStringTemplates
Let me know what you think of it :)
9
u/repeating_bears Oct 30 '23 edited Oct 30 '23
Not sure how I feel about a template working exclusively by side effects. Feels like a processor really ought to produce something.
edit: What IDE are you using? My version of IntelliJ gives the error that
INFO."foo";is not a statement. I reported it as a bug. But I think it's indicative that you're doing something unexpected here.There's also the problem of having to traverse the stack every time you log something, even if that log level is disabled.
Here's what I came up with instead.
4
u/ramdulara Oct 30 '23
Does this mean that the string is always eagerly evaluated? For example in info mode, Debug log messages shouldn't get evaluated at all if possible. How do you ensure that?
3
u/agentoutlier Oct 30 '23
I was playing with something similar in my modern SLF4J implementation: https://github.com/jstachio/rainbowgum
Unfortunately it is not checked in because I had some issues with javadoc failing with JEP 430 and I want to release soon (release is a strong word ... more like just push to central :) ).
I'm also playing with Scoped Values.
I will push both branches later today (locally on a different computer).
7
u/Thihup Oct 30 '23 edited Oct 30 '23
Wouldn't using `MethodHandles.lookup().lookupClass()` be a easier way to get the calling class?
Edit: I was looking for the `StackWalker.getCallerClass()` method
1
u/[deleted] Oct 30 '23
Looks awesome!
9
u/[deleted] Oct 30 '23
[removed] — view removed comment