SLF4J (log4j underneath) - injecting a runtime param into the logger creation?

StackOverflow https://stackoverflow.com/questions/23156784

  •  05-07-2023
  •  | 
  •  

I have a thread of operation which I would like to be logging like so:

id = tied to a "session" which is known at runtime (not 100%, but mostly a session is everything on a sepecific thread)

KEY = value tied to a particular package, probably can just make a different formater pattern with each package/key

12:12:12,123 [INFO] [id] KEY > Something informative
12:12:13,123 [INFO] [id] KEY > Something informative
12:12:14,123 [ERROR] [id] KEY > OMG IT BROKE!
12:12:15,123 [INFO] [id] KEY > Something informative

I already have SLF4J logging, but I'm getting the classic style:

19:18:41,491 INFO  [com.package.package.Class] (ThreadName) Something Informative
19:18:42,491 INFO  [com.package.package.Class] (ThreadName) Something Informative

I realize I can create a different format for the classes I'm after in the log4j/logback/whatever properites/xml file; however, I have no idea how to inject that [id] value which is only known at runtime.

Is there a way to do this "properly" with SLF4J or do I need to create my own Log4J Logger instance at runtime and couple my code to log4j (which I've tried very hard not to do).

有帮助吗?
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top