cf-java-logging-support icon indicating copy to clipboard operation
cf-java-logging-support copied to clipboard

Add StructuredLogFormatter wrapper for JsonEncoder (logback)

Open sustacek opened this issue 7 months ago • 3 comments

Hi, I wanted to be able to use the JsonEncoder for Logback natively with SpringBoot, but for now I always have to create a logback.xml. SpringBoot 3.4 introduced support for structured formatting, without the need to write logback.xml / log4j.xml.

To support his, I created a simple wrapper. Then user will be able to use the encoder as a formatter for structured logging with just a regular application.properties / .yaml, like this:

# application.yaml
logging:
  structured:
    format:
      console: com.sap.hcp.cf.logback.boot.CloudFoundryStructuredLogFormatter
      #console: ecs
  level:
    root: INFO
    com.sap.cloud.sdk: INFO
    com.sap.cloud.security: DEBUG

Let me know what you think. In our application, I've created a similar wrapper, but since JsonEncoder does not expose the getJson(event) method, it's a little bit cumbersome -- I had to use reflection. I didn't want to use the public encode(event) because I'd need to decode it right back using UTF-8 into a String instance, every time.

Alternatively, if you could expose the JsonEncoder.getJson(event) method (or a new one, returning JSON as String), that'd help us a lot as well. I know this commit drags with itself a dependency on SpringBoot, which might not be ideal.

sustacek avatar Jun 18 '25 08:06 sustacek