This is an automated archive made by the Lemmit Bot.
The original was posted on /r/java by /u/darenkster on 2023-10-30 12:22:15.
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:
Let me know what you think of it :)
You must log in or register to comment.