Code Monkey home page Code Monkey logo

loggingwithstringtemplates's Introduction

LoggingWithStringTemplates

Just a proof of concept of how to use the new String Template feature from JDK 21 to use with a logging framework, in this case SLF4J.

The different loggers (info, debug, error etc) are defined in the class LOG as fields and are initialized statically. The calling class ist determined via the StackWalker API, which gets the first class that isn't anonymous or the LOG class. If the StringTemplate contains a Throwable, then the log method with the Throwable parameter is called, else thje log method with only one String parameter.

To use the loggers either call them directly:

import de.darenkster.stringtemplates2slf4j.loggers.LOG;

...

LOG.INFO."Text \{variable}"

or statically import the fields from the LOG class:

import static de.darenkster.stringtemplates2slf4j.loggers.LOG.*;

...

INFO."Text \{variable}"

The Main class prints out the following:

10:33:44.899 [main] INFO de.darenkster.stringtemplates2slf4j.Main -- This is a info test
10:33:44.907 [main] ERROR de.darenkster.stringtemplates2slf4j.Main -- This is a error test 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 test
10:33:44.910 [main] WARN de.darenkster.stringtemplates2slf4j.Main -- This is a warn
test
10:33:44.912 [main] INFO de.darenkster.stringtemplates2slf4j.Main -- This is a info test from within an anonymous class
10:33:44.912 [main] INFO de.darenkster.stringtemplates2slf4j.Main -- This is a info test from within a lambda

loggingwithstringtemplates's People

Contributors

darenkster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.