Where are keycloak logs stored?

Keycloak’s default logger is console logger. If you want your logs to be persisted, you have to explicitly enable file logger.

# using CLI
bin/kc.sh start-dev --log=file

# using environment variable
export KC_LOG=file
bin/kc.sh start-dev

Once file logger enabled, log files will be created in the below directory

$KEYCLOAK_HOME/data/log/

To learn how to use logs effectively, continue reading to discover more.

How to use multiple log handlers?

Keycloak supports 3 types of log handlers.

  • console
  • file
  • syslog

Above 3 types are supported by Keycloak version 26. Supported log handlers can be changed with the Keycloak version that you are using.

To set multiple log handlers, you can add them together, separated with a comma.

# using CLI
bin/kc.sh start-dev --log="file,console"

# using environment variables
export KC_LOG="file,console"
bin/kc.sh start-dev

How to set log file location?

# using CLI
bin/kc.sh start-dev --log="file" --log-file=<path-to>/<your-file.log>

# using environment variables
export KC_LOG="file,console"
export KC_LOG_FILE=<path-to>/<your-file.log>
bin/kc.sh start-dev

How to set log levels?

LevelDescription
FATALCritical failures with complete inability to serve any kind of request.
ERRORA significant error or problem leading to the inability to process requests.
WARNA non-critical error or problem that might not require immediate correction.
INFOKeycloak lifecycle events or important information. Low frequency.
DEBUGMore detailed information for debugging purposes, such as database logs. Higher frequency.
TRACEMost detailed debugging information. Very high frequency.
ALLSpecial level for all log messages.
OFFSpecial level to turn logging off entirely (not recommended).
Source: https://www.keycloak.org/server/logging

Setting root log level

# using CLI
bin/kc.sh start-dev --log-level=DEBUG #set root level

# using environment variables
export KC_LOG_LEVEL=DEBUG #set root level
bin/kc.sh start-dev

Setting log level for each handler

# using CLI
bin/kc.sh start-dev --log-console-level=warn --log-file-level=debug --log-syslog-level=info

# using environment variables
export KC_LOG_FILE_LEVEL=DEBUG
export KC_LOG_CONSOLE_LEVEL=WARN
export KC_LOG_SYSLOG_LEVEL=INFO
bin/kc.sh start-dev

Note that above handler level loggers never override the root log level. Handler specific log levels should always be lower or equal to the root log level

Setting category specific log levels

You can set different log levels for different areas ( java package) of the Keycloak code.

Package based different log levels should be comma separated. Log level is added after the semicolon.

# using CLI
bin/kc.sh start-dev --log-level="INFO,org.hibernate:debug,org.hibernate.hql.internal.ast:info"

# using environment variables
export KC_LOG_LEVEL="INFO,org.hibernate:debug,org.hibernate.hql.internal.ast:info"
bin/kc.sh start-dev

Explanation to the above snippet

Root level logger is set to INFO

Different log levels are set to each package

How to set log format?

SymbolSummaryDescription
%%%Renders a simple % character.
%cCategoryRenders the log category name.
%d{xxx}DateRenders a date with the given date format string.String syntax defined by java.text.SimpleDateFormat
%eExceptionRenders a thrown exception.
%hHostnameRenders the simple host name.
%HQualified host nameRenders the fully qualified hostname, which may be the same as the simple host name, depending on the OS configuration.
%iProcess IDRenders the current process PID.
%mFull MessageRenders the log message and an exception, if thrown.
%nNewlineRenders the platform-specific line separator string.
%NProcess nameRenders the name of the current process.
%pLevelRenders the log level of the message.
%rRelative timeRender the time in milliseconds since the start of the application log.
%sSimple messageRenders only the log message without exception trace.
%tThread nameRenders the thread name.
%t{id}Thread IDRender the thread ID.
%z{<zone name>}TimezoneSet the time zone of log output to <zone name>.
%LLine numberRender the line number of the log message.
Source: https://www.keycloak.org/server/logging
# using CLI
bin/kc.sh start-dev --log-console-format="'%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n'"

# using environment variables
export KC_LOG_CONSOLE_FORMAT="'%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n'"
bin/kc.sh start-dev 

Thank you for reading to the end of the post. I hope you gained a basic understanding of Keycloak logs. If you want to learn more, you can check Keycloak’s documentation on logging.

Thank you . . .

References

https://www.keycloak.org/server/logging

https://www.keycloak.org/server/all-config


Posted

in

by