Keycloak on kubernetes and logging json layaout format with log4j2

7/26/2019

I have Keycloak deployed in Kubernetes using the official codecentric chart. Now i want to make Keycloak logs into json format in order to exploit them in Kibana. Any one has an idea ?

-- REDOUANE
jboss
json
keycloak
kubernetes
log4j

2 Answers

1/16/2020

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

  cli:
# Custom CLI script
custom: |
  /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)
-- REDOUANE
Source: StackOverflow

7/26/2019

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.ConsoleHandler
handler.CONSOLE.level=INFO
handler.CONSOLE.formatter=COLOR-PATTERN
handler.CONSOLE.properties=autoFlush,target,enabled
handler.CONSOLE.autoFlush=true
handler.CONSOLE.target=SYSTEM_OUT
handler.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.JsonFormatter
formatter.json.properties=keyOverrides,exceptionOutputType,metaData,prettyPrint,printDetails,recordDelimiter
formatter.json.constructorProperties=keyOverrides
formatter.json.keyOverrides=timestamp\=@timestamp
formatter.json.exceptionOutputType=FORMATTED
formatter.json.metaData=@version\=1
formatter.json.prettyPrint=false
formatter.json.printDetails=false
formatter.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.

-- Utku Ă–zdemir
Source: StackOverflow