Skip to content

Navigation Menu

Sign up

docs: add Structured Logging section to README #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
afeld wants to merge 1 commit into GoogleCloudPlatform:main
base: main
Choose a base branch
Loading
from afeld:docs/structured-logging
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /