0

Problem: Keycloak logs stack traces in exceptions to multiple lines and I'd rather have the stack trace customised (ie. separate by some arbitrary character, say |). The logs are sent to Splunk for later perusal and I'd rather have the whole exception logged (with the full stack trace) as one line / event there as well.

Example of log generated by Keycloak:

2023-08-22 07:19:22,081 ERROR [com.example.BaseUserProvider] (executor-thread-0) Error getting user: org.apache.http.conn.HttpHostConnectException: Connect to host.docker.internal:8080 [host.docker.internal/192.168.65.254] failed: Connection refused (Connection refused)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)

I am a bit lost as how to customise the stack trace. Per Keycloak documentation, log format can be customised but that doesn't have an option to customise the exception format, see Keycloak documentation: https://www.keycloak.org/server/logging mentions this:

Keycloak uses the JBoss Logging framework. The following is a high-level overview for the available log handlers:

  • root

    • console (default)

I am using Keycloak 20.0.5 (official Docker image) as base.

Laurenzo
  • 505
  • 3
  • 20
  • There is no way to do that. The stack traces are formatted similar to `Throwable.printStackTrace()`. Are you pushing logs to a log aggregator? – James R. Perkins Aug 22 '23 at 16:17
  • Hi @JamesR.Perkins and thanks for your comment! Can you elaborate a bit as why and where the formatting is done this way? And possibly add an answer to this question so I can accept it. Thanks! – Laurenzo Aug 23 '23 at 04:05
  • @JamesR.Perkins yes, I am sending these logs to Splunk. – Laurenzo Aug 23 '23 at 04:32
  • That's just how stack traces have generally always been formatted. You could use a structure like the `org.jboss.logmanager.formatters.JsonFormatter` to put them in a structured format. Short of that, you'd need to write your own `java.util.logging.Formatter` to get them in a format you'd prefer. – James R. Perkins Aug 23 '23 at 19:22
  • Right. I was just wondering then could I somehow configure Keycloak to use different logging library such as Log4J2 and then configure the stack trace formatting like this: https://stackoverflow.com/a/43159613/3489098 – Laurenzo Aug 24 '23 at 05:59
  • Sorry, I don't know enough about Keycloak to know that. I'm pretty sure all the containers it run in use the JBoss Log Manager. A feature request could be filed at https://issues.redhat.com/browse/LOGMGR, though I know that's not an immediate solution. – James R. Perkins Aug 24 '23 at 14:40

0 Answers0