Afficher les dernières lignes des entrées de journaux
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Explique comment afficher les dernières entrées des journal en temps réel.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["# Tail log entries\n\nDemonstrates how to tail live log entries.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Streaming and live tailing log entries](/logging/docs/view/streaming-live-tailing)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tlogging \"cloud.google.com/go/logging/apiv2\"\n \t\"cloud.google.com/go/logging/apiv2/loggingpb\"\n )\n\n // tailLogs creates a channel to stream log entries that were recently ingested for a project\n func tailLogs(projectID string) error {\n \t// projectID := \"your_project_id\"\n\n \tctx := context.Background()\n \tclient, err := logging.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/apiv2.html#cloud_google_com_go_logging_apiv2_Client_NewClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewClient error: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tstream, err := client.TailLogEntries(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"TailLogEntries error: %w\", err)\n \t}\n \tdefer stream.CloseSend()\n\n \treq := &loggingpb.TailLogEntriesRequest{\n \t\tResourceNames: []string{\n \t\t\t\"projects/\" + projectID,\n \t\t},\n \t}\n \tif err := stream.Send(req); err != nil {\n \t\treturn fmt.Errorf(\"stream.Send error: %w\", err)\n \t}\n\n \t// read and print two or more streamed log entries\n \tfor counter := 0; counter \u003c 2; {\n \t\tresp, err := stream.Recv()\n \t\tif err == io.EOF {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"stream.Recv error: %w\", err)\n \t\t}\n \t\tfmt.Printf(\"received:\\n%v\\n\", resp)\n \t\tif resp.Entries != nil {\n \t\t\tcounter += len(resp.Entries)\n \t\t}\n \t}\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntry.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntryServerStream.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.TailOption.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html;\n\n public class TailLogEntries {\n\n public static void main(String[] args) throws Exception {\n // TODO(developer): Optionally provide the logname as an argument.\n String logName = args.length \u003e 0 ? args[0] : \"\";\n\n https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html options = https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html#com_google_cloud_logging_LoggingOptions_getDefaultInstance__();\n try (https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html logging = options.getService()) {\n\n // Optionally compose a filter to tail log entries only from specific log\n https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntryServerStream.html stream;\n\n if (logName != \"\") {\n stream =\n logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html#com_google_cloud_logging_Logging_tailLogEntries_com_google_cloud_logging_Logging_TailOption____(\n https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.TailOption.html.filter(\n \"logName=projects/\" + options.getProjectId() + \"/logs/\" + logName));\n } else {\n stream = logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html#com_google_cloud_logging_Logging_tailLogEntries_com_google_cloud_logging_Logging_TailOption____();\n }\n System.out.println(\"start streaming..\");\n for (https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntry.html log : stream) {\n System.out.println(log);\n // cancel infinite streaming after receiving first entry\n stream.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntryServerStream.html#com_google_cloud_logging_LogEntryServerStream_cancel__();\n }\n }\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const {Logging} = require('https://cloud.google.com/nodejs/docs/reference/logging/latest/overview.html');\n const logging = new https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html();\n\n /**\n * TODO(developer): Replace logName with the name of your log.\n */\n const log = logging.log(logName);\n console.log('running tail log entries test');\n\n const stream = log\n .tailEntries({\n filter: 'timestamp \u003e \"2021-01-01T23:00:00Z\"',\n })\n .on('error', console.error)\n .on('https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/entry.html', resp =\u003e {\n console.log(resp.entries);\n console.log(resp.suppressionInfo);\n // If you anticipate many results, you can end a stream early to prevent\n // unnecessary processing and API requests.\n https://cloud.google.com/nodejs/docs/reference/logging-bunyan/latest/logging-bunyan/loggingbunyan.html.end();\n })\n .on('end', () =\u003e {\n console.log('log entry stream has ended');\n });\n\n // Note: to get all project logs, invoke logging.tailEntries\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=logging)."]]