Skip to content

Commit 12420e0

Browse files
authored
Document structlog_gcp.build_processors (#44)
1 parent 7e268c4 commit 12420e0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ if not converted:
6060
logger.critical("This is not supposed to happen", converted=converted)
6161
```
6262

63+
The `structlog_gcp.build_processors()` function constructs structlog processors to:
64+
65+
* Output logs as Google Cloud Logging format using the default Python JSON serializer.
66+
* Carry context variables across loggers (see [structlog: Context Variables](https://www.structlog.org/en/stable/contextvars.html))
67+
68+
For more advanced usage, see [Advanced Configuration][#advanced-configuration]
69+
70+
6371
### Errors
6472

6573
Errors are automatically reported to the [Google Error Reporting service](https://cloud.google.com/error-reporting/).
@@ -81,6 +89,49 @@ You can configure the service name and the version used during the report with 2
8189
structlog.configure(processors=processors)
8290
```
8391

92+
### Advanced Configuration
93+
94+
If you need to have more control over the processors configured by the library, you can use the `structlog_gcp.build_gcp_processors()` builder function.
95+
96+
This function only configures the Google Cloud Logging-specific processors and omits all the rest.
97+
98+
In particular, you can use this function:
99+
100+
* If you want to have more control over the processors to be configured in structlog. You can prepend or append other processors around the Google-specific ones.
101+
* If you want to serialize using another JSON serializer or with specific options.
102+
103+
For instance:
104+
105+
106+
```python
107+
import orjson
108+
import structlog
109+
from structlog.processors import JSONRenderer
110+
111+
import structlog_gcp
112+
113+
114+
def add_open_telemetry_spans(...):
115+
# Cf. https://www.structlog.org/en/stable/frameworks.html#opentelemetry
116+
...
117+
118+
gcp_processors = structlog_gcp.build_gcp_processors()
119+
120+
# Fine-tune processors
121+
processors = [add_open_telemetry_spans]
122+
processors.extend(gcp_processors)
123+
processors.append(JSONRenderer(serializer=orjson.dumps))
124+
125+
structlog.configure(processors=processors)
126+
```
127+
128+
> [!IMPORTANT]
129+
>
130+
> `structlog_gcp.build_gcp_processors()` **doesn't** configure a renderer and
131+
> you must supply a JSON renderer of your choice for the library to work
132+
> correctly.
133+
134+
84135
## Examples
85136

86137
Check out the [`examples` folder](https://github.com/multani/structlog-gcp/tree/main/examples) to see how it can be used.

0 commit comments

Comments
 (0)