Solved

Custom log4j2 formatting in Service Framework

  • 13 September 2022
  • 3 replies
  • 55 views

I’m trying to create a custom log format in the Service Framework - specifically, include a GUID in each log line.

I *think* I can do that using a formatter (java.util.logging.Formatter), but can't figure out how that would work. Are there any examples around I could steal?

Thanks!

icon

Best answer by marc 19 September 2022, 14:32

View original

3 replies

Userlevel 2
Badge +1

Hi Marc,

Sorry for not getting back sooner, are you referring to just adding a unique id for each log message, or do you need GUIDs coming from a database to appear in the logging?

If its the former, I think you can configure additional formatting via the log4j2.xml, specifically looking at UService.log: 

<RollingFile name="UService_Appender" fileName="./logs/UService.log" filePattern="./logs/UService.log.%i.log">
            <PatternLayout pattern="%d{ISO8601} %-5p %t %C %x - %m%n"/>

In this example on SO :

https://stackoverflow.com/questions/43999342/how-to-add-uuid-to-log4j-for-logging-into-file

they are using  UUID:%u{RANDOM}

If you add that to the PatternLayout , you should get unique id's for every log message in the UService.log.

		<RollingFile name="UService_Appender" fileName="./logs/UService.log" filePattern="./logs/UService.log.%i.log">
<PatternLayout pattern="UUID:%u{RANDOM} %d{ISO8601} %-5p %t %C %x - %m%n"/>

 

Exactly what I was looking for, Dara, thanks! Somehow an hour of searching never got me to this answer...

Userlevel 2
Badge +1

ahha glad it helps Marc !!! 😄 

Reply