Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,35 @@ documentation on

See the [running example](examples/cloud_run_event).

## Structured Logging

When running in Cloud Run or Cloud Run Functions, log entries written as plain
text to stdout won't have a severity level parsed by Cloud Logging. To enable
structured logging with correct severity and automatic trace correlation, install
[`google-cloud-logging`](https://pypi.org/project/google-cloud-logging/) and
attach its `StructuredLogHandler` to the Python root logger:

```python
import logging
import functions_framework
from google.cloud.logging_v2.handlers import StructuredLogHandler

logging.basicConfig(handlers=[StructuredLogHandler()], level=logging.INFO)


@functions_framework.http
def hello(request):
logging.info("Processing request")
return "Hello world!"
```

`StructuredLogHandler` formats each log record as a JSON object with a
`severity` field, which Cloud Logging uses to set the entry's severity level.
When inside a request handler, it also automatically populates
`logging.googleapis.com/trace` and `spanId` from the `X-Cloud-Trace-Context`
request header so your function's log entries are correlated with the parent
request trace.

## Advanced Examples

More advanced guides can be found in the [`examples/`](examples/) directory.
Expand Down