[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-03。"],[[["\u003cp\u003eJava's standard output and error streams are propagated to Cloud Logging, but without granular severity levels.\u003c/p\u003e\n"],["\u003cp\u003eThe Java Logging API (JUL) should be used to ensure correct log severity levels are propagated to Cloud Logging.\u003c/p\u003e\n"],["\u003cp\u003eOther logging frameworks like SLF4J, Log4j 2, and Apache Commons Logging require specific configurations, either via structured JSON formatting or bridges/adapters, to achieve proper severity level propagation.\u003c/p\u003e\n"],["\u003cp\u003eSpring Cloud Run functions using the Google Cloud Adapter can utilize the \u003ccode\u003eCONSOLE_JSON\u003c/code\u003e appender for correct log formatting and utilize SLF4J with Logback which can be routed to Java Logging API.\u003c/p\u003e\n"],["\u003cp\u003eWhen using alternative logging frameworks, it is crucial to remove any existing bridges that may interfere with the correct formatting of log messages and severity levels.\u003c/p\u003e\n"]]],[],null,["# Java Logging\n============\n\nIn the Java runtime, any output to `STDOUT` or `STDERR` is propagated to [Cloud\nLogging](/functions/1stgendocs/monitoring/logging). However, granular log severity\nlevel is not propagated. Use the standard [Java Logging API (JUL)](https://docs.oracle.com/javase/10/core/java-logging-overview.htm/) to log messages and propagate the\ncorrect log severity levels to Cloud Logging.\n\nIf you use a different logging framework, such as [SLF4J](http://www.slf4j.org/), [Log4j 2](https://logging.apache.org/log4j/2.x/), or [Apache Commons Logging](https://commons.apache.org/proper/commons-logging/), you can propagate the correct log\nseverity level in one of the following ways:\n\n- Format the log messages in the [Structured Logging JSON format](/logging/docs/structured-logging) so that the severity level is propagated to Cloud Logging.\n- 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.\n\nIf your function uses different libraries and the libraries use different\nlogging frameworks, then you'll need to configure each of the logging frameworks\nused. The following sections cover some of the various options.\n\nSpring Cloud Run function\n-------------------------\n\nIf you use Spring Cloud Run function with the Spring Cloud Run functions\nGoogle Cloud Adapter, you can output the log in the right format using the\n[Spring Cloud's Google Cloud Logging Starter](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-gcp-logging-sample) and the\n[`CONSOLE_JSON` appender](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/blob/main/spring-cloud-gcp-logging/src/main/resources/com/google/cloud/spring/logging/logback-json-appender.xml)\nto output the log in the JSON format. Spring uses SLF4J and Logback by\ndefault---but you can also route SLF4J logs to the Java Logging API. See the\n[SLF4J](#slf4j) section for details.\n\nJava Logging API (JUL)\n----------------------\n\nAll supported\nCloud Run functions [Java\nenvironments](/functions/1stgendocs/runtime-support#java)\ninclude Java Logging with a JSON handler that outputs the log in the correct\nformat. Some Java frameworks may add additional components to bridge JUL to a\ndifferent logging framework (such as [jul-to-slf4j bridge](http://www.slf4j.org/legacy.html#jul-to-slf4j) or [Log4j JDK Logging Adapter](https://logging.apache.org/log4j/2.0/log4j-jul.html)), in which case those log\nmessages will no longer be formatted correctly. If your log messages do not have\ncorrect severity levels, make sure those bridges are removed.\n\nSLF4J\n-----\n\n[SLF4J](http://www.slf4j.org/) can write logs to the\nJava Logging API by using the `slf4j-jdk14` logging framework binding. See the\n[SLF4J\ndocumentation](http://www.slf4j.org/docs.html) for details. Make sure you don't have the `jul-to-slf4j`\nbridge.\n\nLogback\n-------\n\n[Logback](http://logback.qos.ch/) does not have an\nappender that routes log messages to the Java Logging API. However, Logback is\nusually used with the SFL4J API, so you can switch the\nSLF4J logger implementation to the [Java Logging API](#java_logging_api_jul).\n\nLog4j 2\n-------\n\n[Log4j 2](https://logging.apache.org/log4j/2.x/)\ndoes not have a direct adapter that routes log messages to the Java Logging API.\nIf you are using Log4j 2, then you need to first route messages to SLF4J using\nthe [Log4J 2 to SLF4J Adapter](https://logging.apache.org/log4j/log4j-2.2/log4j-to-slf4j/index.html), then configure SLF4J to use the\n[Java Logging API](#java_logging_api_jul) implementation. Make sure you do not\nhave the Log4j JDK Logging Adapter.\n\nApache Commons Logging (JCL)\n----------------------------\n\n[Apache Commons Logging](https://commons.apache.org/proper/commons-logging/) has a Java Logging API adapter.\nConfigure Commons Logging to use the [Jdk14Logger](http://commons.apache.org/proper/commons-logging/apidocs/org/apache/commons/logging/impl/Jdk14Logger.html). See the [Apache Commons Logging configuration](http://commons.apache.org/proper/commons-logging/guide.html#Configuration) page for more\ninformation."]]