0

I am being new to the spring-boot application, I have a scenario where I need to add a string as a prefix for existing loggers in the current application. I tried this solution, but it didn't work for me. Can anyone help me out with this?

ex: logger.info("Started Test Application...") ==> Started Test Application... need to add a string of "Amigo" as a prefix ==> Amigo Started Test Application...

mostly I wanted to add that string to all existing logs at one go. any simple solution instead of adding manually to all loggers.

Taylor
  • 3,942
  • 2
  • 20
  • 33

1 Answers1

0

It's a little unclear what your current log setup is, but I'll assume you've got a vanilla out of the box spring boot logging (using default logback impl, no logging config in place).

First, the ref doc for this is found here: https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-logging.html

What you're trying to do is customize the pattern or layout. The docs for doing this in logback (the spring boot default logging impl) are here: https://logback.qos.ch/manual/layouts.html

The easiest way is to add the property logging.pattern.console to your application.properties. Note that if you have an additional logging config in your app (e.g. there's a file named logback.xml or similar in your app) this won't work.

The default console pattern for spring boot is, I believe,

%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40logger{1.} : %m%n%wEx

So to Add what you want would be something like:

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40logger{1.} : Amigo %m%n%wEx

Note, I haven't tested this so you may need to fiddle a bit.

Taylor
  • 3,942
  • 2
  • 20
  • 33