3

I have Keycloak deployed in Kubernetes using the official codecentric chart. Now I want to make Keycloak logs into json format in order to export them to Kibana.

pppery
  • 3,731
  • 22
  • 33
  • 46
REDOUANE
  • 81
  • 2
  • 6

2 Answers2

5

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)
Andre de Miranda
  • 726
  • 7
  • 17
REDOUANE
  • 81
  • 2
  • 6
1

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.

James R. Perkins
  • 16,800
  • 44
  • 60
Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49
  • 1
    Thank you for your help. In fact the logging.properties file is generated from xml configurations after each server startup. So once the Jboss started, the content of the file with json formatter is crushed – REDOUANE Jul 30 '19 at 22:43
  • This answer is not correct. The WildFly documentation is quite explicit about not making changes to logging.properties manually https://docs.jboss.org/author/display/WFLY10/Logging+Configuration#LoggingConfiguration-Whyistherea%7B%7Blogging.properties%7D%7Dfile%3F – Andre de Miranda Oct 14 '19 at 08:27
  • I suspect the following reply provide a better solution https://stackoverflow.com/a/54522501/888876 – Andre de Miranda Oct 14 '19 at 09:13
  • 1
    @AndredeMiranda you're right. Finally, there is a jboss cli command to do this. it will be executed automatically in the starting of the server and we can pass it throw the keycloak helm chart – REDOUANE Jan 16 '20 at 10:34