Se stai creando una nuova funzione, consulta la guida rapida della console su Cloud Run. I contenuti di questa pagina si applicano solo alle funzioni legacy esistenti create con l'API Cloud Functions v1.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Logging Java
Nel runtime Java, qualsiasi output in STDOUT o STDERR viene propagato a Cloud Logging. Tuttavia, il livello di gravità dei log granulare non viene propagato. Utilizza l'API Java Logging (JUL) standard per registrare i messaggi e propagare i livelli di gravità dei log corretti a Cloud Logging.
Se utilizzi un framework di logging diverso, ad esempio SLF4J, Log4j 2 o Apache Commons Logging, puoi propagare il livello di gravità corretto dei log in uno dei seguenti modi:
Utilizza un bridge/adattatore per inviare i messaggi di log all'API Java Logging, che già propaga i messaggi di log nel formato corretto. Se scegli questo approccio, assicurati che non esista già un'entità di adattamento che inoltri già dall'API Java Logging al framework di logging.
Se la funzione utilizza librerie diverse e queste ultime utilizzano diversi framework di logging, dovrai configurare ciascuno dei framework di logging utilizzati. Le sezioni seguenti illustrano alcune delle varie opzioni.
Tutti gli ambienti Java supportati per le funzioni Cloud Run includono il logging Java con un gestore JSON che genera il log nel formato corretto. Alcuni framework Java potrebbero aggiungere componenti aggiuntivi per collegare JUL a un altro framework di logging (ad esempio jul-to-slf4j bridge o Log4j JDK Logging Adapter), nel qual caso i messaggi di log non saranno più formattati correttamente. Se i messaggi di log non hanno livelli di gravità corretti, assicurati che i ponti vengano rimossi.
SLF4J
SLF4J può scrivere log nell'API Logging di Java utilizzando il binding del framework di logging slf4j-jdk14. Per ulteriori dettagli, consulta la documentazione di SLF4J. Assicurati di non avere il jul-to-slf4j
bridge.
Logback
Logback non ha un appender che inoltra i messaggi di log all'API Java Logging. Tuttavia, Logback viene solitamente utilizzato con l'API SFL4J, quindi puoi passare dall'implementazione del logger SLF4J all'API Java Logging.
Log4j 2
Log4j 2
non dispone di un'opzione diretta che inoltra i messaggi di log all'API Java Logging.
Se utilizzi Log4j 2, devi prima inoltrare i messaggi a SLF4J utilizzando
l'adattatore Log4J 2 a SLF4J, quindi configurare SLF4J per utilizzare l'implementazione dell'API Java Logging. Assicurati di non avere l'adattatore di logging Log4j JDK.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-03 UTC."],[[["\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."]]