Java Logging

In the Java runtime, any output to STDOUT or STDERR is propagated to Cloud Logging. However, granular log severity level is not propagated. Use the standard Java Logging API (JUL) to log messages and propagate the correct log severity levels to Cloud Logging.

If you use a different logging framework, such as SLF4J, Log4j 2, or Apache Commons Logging, you can propagate the correct log severity level in one of the following ways:

  • Format the log messages in the Structured Logging JSON format so that the severity level is propagated to Cloud Logging.
  • Use a bridge/adapter to send log messages to the Java Logging API, which already propagates the log messages in the correct format. If you take this approach, make sure there is not an existing adapter that already routes from the Java Logging API to the logging framework.

If your function uses different libraries and the libraries use different logging frameworks, then you'll need to configure each of the logging frameworks used. The following sections cover some of the various options.

Spring Cloud Run function

If you use Spring Cloud Run function with the Spring Cloud Run functions Google Cloud Adapter, you can output the sign in the right format using the Spring Cloud's Google Cloud Logging Starter and the CONSOLE_JSON appender to output the sign in the JSON format. Spring uses SLF4J and Logback by default—but you can also route SLF4J logs to the Java Logging API. See the SLF4J section for details.

Java Logging API (JUL)

All supported Cloud Run functions Java environments include Java Logging with a JSON handler that outputs the log in the correct format. Some Java frameworks may add additional components to bridge JUL to a different logging framework (such as jul-to-slf4j bridge or Log4j JDK Logging Adapter), in which case those log messages will no longer be formatted correctly. If your log messages do not have correct severity levels, make sure those bridges are removed.

SLF4J

SLF4J can write logs to the Java Logging API by using the slf4j-jdk14 logging framework binding. See the SLF4J documentation for details. Make sure you do not have the jul-to-slf4j bridge.

Logback

Logback does not have an appender that routes log messages to the Java Logging API. However, Logback is usually used with the SFL4J API, so you can switch the SLF4J logger implementation to the Java Logging API.

Log4j 2

Log4j 2 does not have a direct adapter that routes log messages to the Java Logging API. If you are using Log4j 2, then you need to first route messages to SLF4J using the Log4J 2 to SLF4J Adapter, then configure SLF4J to use the Java Logging API implementation. Make sure you do not have the Log4j JDK Logging Adapter.

Apache Commons Logging (JCL)

Apache Commons Logging has a Java Logging API adapter. Configure Commons Logging to use the Jdk14Logger. See the Apache Commons Logging configuration page for more information.