Louder's structured logging and tracing library
Project description
Louder Logging
A Google Cloud Logging compliant structured logging library.
Logging Setup
The following environment variables must be set for logging to function when using
the provided ldr.logging.Entry type.
# The name of the actual application running, e.g cloud run service / job name
export LDR_APPLICATION_NAME=application-name
# The version of the application, example here for poetry environments
export LDR_APPLICATION_VERSION=$(poetry version -s)
# The name of the service, can be the same as application name if the service is only
# one component, but in larger projects will be the name of the service offering rather
# than just the application name.
export LDR_APPLICATION_SERVICE_NAME=application-service-name
# The commit hash that this version of the code was committed with.
export LDR_APPLICATION_COMMIT_HASH=$(git rev-parse HEAD)
These can be omitted when using custom structured logs, or unstructured logs.
Examples
import ldr.logging
import logging
ldr.logging.setup(logging.DEBUG)
logging.info("Hello, world!")
entry = ldr.logging.Entry()
logging.warning()
Will write the following to stderr
{"logging.googleapis.com/diagnostic": {"instrumentation_source": [{"name": "python", "version": "3.11.3"}]},"severity": "INFO", "logging.googleapis.com/labels": {"python_logger": "google.cloud.logging_v2.handlers.structured_log"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 149, "file": "/Users/you/Library/Caches/pypoetry/virtualenvs/my-application-N_XRT3hE-py3.11/lib/python3.11/site-packages/google/cloud/logging_v2/handlers/structured_log.py", "function": "emit_instrumentation_info"}, "httpRequest": {} }
{"message": "Hello, world!","severity": "INFO", "logging.googleapis.com/labels": {"python_logger": "root"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 6, "file": "/Users/you/project/src/main.py", "function": "<module>"}, "httpRequest": {} }
{"client": "LOUD", "message": "Something weird happened but we've handled it", "app": {"name": "application-name", "version": "0.1.0", "service_name": "application-service-name", "commit_hash": "ea09ae9cc6768c50fcee903ed054556e5bfc8347"}, "extra": {"foo": "bar"},"severity": "WARNING", "logging.googleapis.com/labels": {"python_logger": "root"}, "logging.googleapis.com/trace": "", "logging.googleapis.com/spanId": "", "logging.googleapis.com/trace_sampled": false, "logging.googleapis.com/sourceLocation": {"line": 13, "file": "/Users/you/project/src/main.py", "function": "<module>"}, "httpRequest": {} }
Tracing Setup
Tracing is gated behind the tracing extra. Install with pip install 'ldr-logging[tracing]'.
Tracing requires an instance of ldr.logging.TraceConfig. You can set the following env vars to use the
ldr.logging.TraceConfig.from_env method.
# The name of the actual application running, e.g cloud run service / job name
export LDR_APPLICATION_NAME=my-application-auth
# Optional: The application namespace, if the app is a component of a larger application
export LDR_APPLICATION_NAMESPACE=my-application
# Optional: A regex expression to specify which open telemetry resource attributes to propagate.
# This example will export all 'service.{key}' attributes.
# When unset, no resource attributes will be collected.
export LDR_OTEL_INCLUDE_RESOURCE_ATTRIBUTES_REGEX='^service\..*'
Examples
Configure tracing
import ldr.logging
ldr.logging.setup_tracing(ldr.logging.TraceConfig.from_env())
Configure tracing for a FastAPI app.
# pip install fastapi opentelemetry-instrumentation-fastapi ldr-environment
import ldr.environment
import ldr.logging
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
trace_config = ldr.logging.TraceConfig.from_env()
app = FastAPI(name=trace_config.service_name)
ldr.logging.setup_tracing(
config=trace_config,
attributes={
"deployment.environment": ldr.environment.get("ENVIRONMENT", "dev"),
},
instrumentor=FastAPIInstrumentor.instrument_app,
app=app,
)
Copyright
This project is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ldr_logging-0.2.0.tar.gz.
File metadata
- Download URL: ldr_logging-0.2.0.tar.gz
- Upload date:
- Size: 76.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f242e5170019cd412187cf182527b6edf53c9ebc181fcfd731331b3a32af4e8e
|
|
| MD5 |
cfe6623190c9d42df63f43890decdcef
|
|
| BLAKE2b-256 |
fff06b23da89ac3c2f0b184bf013347b8a880d5a96b10ff55e6e1631c7bb12f9
|
File details
Details for the file ldr_logging-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ldr_logging-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
129fc19c54735d818c15347e9a8f0dabf9c16f4039b48bb99cb7c7bbd94706a2
|
|
| MD5 |
a3cf4f6fc7f7d51c99e1603ee7ef2bd1
|
|
| BLAKE2b-256 |
6a2320dfade72ce23d7658b288f1b5f6ae735793159a6bc4ba5779344e08fed8
|