Logs in JSON with custom fields which are dynamic

I’m trying to log in JSON format using --log-console-output=json, which works very well. Thanks for including this functionality.

{ "timestamp": "2022-02-25T10:31:32.452+01:00", "sequence": 8442, "loggerClassName": "org.jboss.logging.Logger", "loggerName": "io.quarkus", "level": "INFO", "message": "Keycloak 18.0.0-SNAPSHOT on JVM (powered by Quarkus 2.7.2.Final) started in 3.253s. Listening on: http://0.0.0.0:8080", "threadName": "main", "threadId": 1, "mdc": {}, "ndc": "", "hostName": "host-name", "processName": "QuarkusEntryPoint", "processId": 36946 }

However, I’m facing two main issues with the above log:

  1. Creating New Fields outside of MDC block:
  • I tried using the property quarkus.log.json.fields.mdc.flat-fields=true to flatten MDC fields, but it isn’t working and the other ways I tried are static (additional-field) and cannot be changed for every request (eg ipAddress)
  1. Formatting Stacktraces:
    I attempted to create new fields using the following configuration:
    'KC_LOG_CONSOLE_FORMAT': '{"EventTime":"%d{yyyy-MM-dd HH:mm:ss,SSS}","level":"%-5p","logger":"%c","thread":"%t","requestId":"%X{requestId}","event":"%s","stackTrace": "%s%E"}%n'

Could anyone provide input on how I could deal with these issues, specifically:

  • Flattening MDC fields or
  • Formatting stacktraces to appear in a single line or in JSON format?
    Thank you!

If I remember correctly, I think I fixed the Exception format in the Json format using this Quarkus specific config Logging configuration - Quarkus

Even though the Keycloak guide advices against, it is very simple to include a quarkus properties files in your Keycloak docker deployment to customize everything in the underlying Quarkus on which Keycloak runs (as it is a Quarkus app after all). And there are things which cannot be done only from Keycloak config, sometimes you need Quarkus specific configs. Like these advanced logging configuration, or for example I customized the Quarkus access logs to be written in a JSON format, or these exception customization, or other customizations related to header process and whatnot.

Anyway, hope is a hint in the correct direction! Good luck