File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed
opentelemetry-exporter-gcp-logging
src/opentelemetry/exporter/cloud_logging Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 6868
6969 logger1.warning(" string log %s " , " here" )
7070
71+ If your code is running in a GCP environment with a supported Cloud Logging agent (like GKE,
72+ Cloud Run, GCE, etc.), you can write logs to stdout in Cloud Logging `structured JSON format
73+ <https://cloud.google.com/logging/docs/structured-logging> `_. Pass the ``structured_json_file ``
74+ argument and use ``SimpleLogRecordProcessor ``:
75+
76+ .. code :: python
77+
78+ import sys
79+ from opentelemetry.exporter.cloud_logging import (
80+ CloudLoggingExporter,
81+ )
82+ from opentelemetry._logs import set_logger_provider
83+ from opentelemetry.sdk._logs import LoggerProvider
84+ from opentelemetry.sdk._logs.export import SimpleLogRecordProcessor
85+
86+ logger_provider = LoggerProvider()
87+ set_logger_provider(logger_provider)
88+ exporter = CloudLoggingExporter(structured_json_file = sys.stdout)
89+ logger_provider.add_log_record_processor(SimpleLogRecordProcessor(exporter))
90+
91+
92+ otel_logger = logger_provider.get_logger(__name__ )
93+ otel_logger.emit(attributes = {" hello" : " world" }, body = {" foo" : {" bar" : " baz" }})
94+
7195 References
7296----------
7397
Original file line number Diff line number Diff line change @@ -220,7 +220,25 @@ def __init__(
220220 client : Optional [LoggingServiceV2Client ] = None ,
221221 * ,
222222 structured_json_file : Optional [TextIO ] = None ,
223- ):
223+ ) -> None :
224+ """Create a CloudLoggingExporter
225+
226+ Args:
227+ project_id: The GCP project ID to which the logs will be sent. If not
228+ provided, the exporter will infer it from Application Default Credentials.
229+ default_log_name: The default log name to use for log entries.
230+ If not provided, a default name will be used.
231+ client: An optional `LoggingServiceV2Client` instance to use for
232+ sending logs. If not provided and ``structured_json_file`` is not provided, a
233+ new client will be created. Passing both ``client`` and
234+ ``structured_json_file`` is not supported.
235+ structured_json_file: An optional file-like object (like `sys.stdout`) to write
236+ logs to in Cloud Logging `structured JSON format
237+ <https://cloud.google.com/logging/docs/structured-logging>`_. If provided,
238+ ``client`` must not be provided and logs will only be written to the file-like
239+ object.
240+ """
241+
224242 self .project_id : str
225243 if not project_id :
226244 _ , default_project_id = google .auth .default ()
You can’t perform that action at this time.
0 commit comments