Search code examples
javaspring-bootlogginglogback

Issue arranging SpringBoot console output log


Small question on how to re arrange a SpringBoot application output log please.

It is well known, if no configuration change, the logs seen in console for a SpringBoot app looks like this:

2023-01-30T21:21:06.076+08:00  INFO 5216 --- [           main] org.example.Main                         : Started Main in 3.544 seconds (process running for 4.151)
2023-01-30T21:21:06.078+08:00 DEBUG 5216 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT

It is a timestamp -> log level -> PID -> --- -> thread -> the class

I just want to rearrange the order to something like:

timestamp -> PID -> log level -> --- -> applicationName -> thread -> the class

Something like:

2023-01-30T21:21:06.076+08:00 5216  INFO --- [myappName][           main] org.example.Main                         : Started Main in 3.544 seconds (process running for 4.151)
2023-01-30T21:21:06.078+08:00 5216 DEBUG --- [myappName][           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT

What I tried:

I saw there is a property for that, and I tried:

logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

However, this is not giving the correct output.

May I ask what is the pattern in order to have this please?

2023-01-30T21:21:06.076+08:00 5216  INFO --- [myappName][           main] org.example.Main                         : Started Main in 3.544 seconds (process running for 4.151)
2023-01-30T21:21:06.078+08:00 5216 DEBUG --- [myappName][           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT

Thank you


Solution

  • Add the following to application.properties:

    logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level ${PID} --- [${spring.application.name}]  [%thread] %logger{36} : %msg%n
    [email protected]@
    

    Sample output:

    2023-01-30 18:21:22 INFO  5340 --- [qualifier]  [main] c.example.demo.QualifierApplication : Starting QualifierApplication using Java 11.0.15 on NB-3HP3HG3 with PID 5340 (C:\workspaces\stackoverflow_workspace\qualifier\target\classes started by WilliamsJ in C:\workspaces\stackoverflow_workspace\qualifier)
    2023-01-30 18:21:22 INFO  5340 --- [qualifier]  [main] c.example.demo.QualifierApplication : No active profile set, falling back to 1 default profile: "default"
    2023-01-30 18:21:22 INFO  5340 --- [qualifier]  [main] o.s.b.w.e.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)