Keycloak on kubernetes and logging json layout format with log4j2 Keycloak on kubernetes and logging json layout format with log4j2 kubernetes kubernetes

Keycloak on kubernetes and logging json layout format with log4j2


A comment to the original reply pointed to a cli command to do this.

  cli:# Custom CLI scriptcustom: |  /subsystem=logging/json-formatter=json:add(exception-output-type=formatted, pretty-print=false, meta-data={label=value})  /subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value=json)


It is a Java application that is running on Wildfly. If you check the main process that is running inside the pod, you will see something like:

/usr/lib/jvm/java/bin/java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.boot.log.file=/opt/jboss/keycloak/standalone/log/server.log -Dlogging.configuration=file:/opt/jboss/keycloak/standalone/configuration/logging.properties -jar /opt/jboss/keycloak/jboss-modules.jar -mp /opt/jboss/keycloak/modules org.jboss.as.standalone -Djboss.home.dir=/opt/jboss/keycloak -Djboss.server.base.dir=/opt/jboss/keycloak/standalone -Djboss.bind.address=10.217.0.231 -Djboss.bind.address.private=10.217.0.231 -b 0.0.0.0 -c standalone.xml

Important part here is the following:

-Dlogging.configuration=file:/opt/jboss/keycloak/standalone/configuration/logging.properties

So, the logging configuration is passed to the Java process as a JVM option, and read from the file on the path /opt/jboss/keycloak/standalone/configuration/logging.properties.

If you check the content of the file, it has a section like the following:

...handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandlerhandler.CONSOLE.level=INFOhandler.CONSOLE.formatter=COLOR-PATTERNhandler.CONSOLE.properties=autoFlush,target,enabledhandler.CONSOLE.autoFlush=truehandler.CONSOLE.target=SYSTEM_OUThandler.CONSOLE.enabled=true...

You need to figure out what to change in this logging configuration to meet your JSON requirements. An example would be:

formatter.json=org.jboss.logmanager.formatters.JsonFormatterformatter.json.properties=keyOverrides,exceptionOutputType,metaData,prettyPrint,printDetails,recordDelimiterformatter.json.constructorProperties=keyOverridesformatter.json.keyOverrides=timestamp\=@timestampformatter.json.exceptionOutputType=FORMATTEDformatter.json.metaData=@version\=1formatter.json.prettyPrint=falseformatter.json.printDetails=falseformatter.json.recordDelimiter=\n

Then, in Kubernetes you can create a ConfigMap with the logging config that you want, define it as a volume in your pod/deployment, and mount it as a file to that exact path in the pod/deployment definition. If you do all steps correctly, you should be able to customize the logging format as you need.