A Python library adding a structured log formatter for Google Cloud
Project description
Structured Logger
Formatter for structured logging in Google Cloud.
Installing
pip install google-cloud-structured-logger
Usage
import logging
from structuredlogger import stlogger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
log_handler = logging.StreamHandler()
formatter = stlogger.StructuredJsonFormatter()
log_handler.setFormatter(formatter)
log_handler.setLevel(logging.DEBUG)
logger.addHandler(log_handler)
Dict config example
import logging.config
from structuredlogger import stlogger
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"structured_json": {
"()": stlogger.StructuredJsonFormatter
}
},
"handlers": {
"default": {
"formatter": "structured_json",
"level": "DEBUG",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
}
},
"root": {
"level": "DEBUG",
"handlers": ["default"]
},
}
logging.config.dictConfig(LOGGING_CONFIG)
Features
severity
is set to the same level as the log- Send log to Error Reporting if log level is
ERROR
,CRITICAL
, orFATAL
- If
exc_info
isTrue
, the stack trace is set tostack_trace
Log example
main.py
:
logger.debug("debug")
logger.info("info", extra={"foo": "bar"})
logger.error("error")
logger.critical("critical")
logger.fatal("fatal")
try:
1 / 0
except Exception as e:
logger.error(e, exc_info=True)
Output:
{"message": "debug", "timestamp": "2022-08-18T12:55:52.064302", "severity": "DEBUG"}
{"message": "info", "foo": "bar", "timestamp": "2022-08-18T12:55:52.064358", "severity": "INFO"}
{"message": "error", "timestamp": "2022-08-18T12:55:52.064381", "severity": "ERROR", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "critical", "timestamp": "2022-08-18T12:55:52.064399", "severity": "CRITICAL", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "fatal", "timestamp": "2022-08-18T12:55:52.064414", "severity": "CRITICAL", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "division by zero", "stack_trace": "Traceback (most recent call last):\n File \"/path/to/main.py\", line 16, in main\n 1 / 0\nZeroDivisionError: division by zero", "timestamp": "2022-08-18T12:55:52.064520", "severity": "ERROR", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
Logs displayed in Google Cloud
Cloud Function:
Cloud Logging:
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
Close
Hashes for google-cloud-structured-logger-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8239a9502da2097f2e69447ac6e48197a046e00de20e12b0a0360d8e1ca0b2e6 |
|
MD5 | a5b4aa345b388155d9da25a3b2f1cca3 |
|
BLAKE2b-256 | c0127c28b770ecb884efdb53691f777dd44cf1347ec2035f0a62b032f70dc919 |
Close
Hashes for google_cloud_structured_logger-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e3031c47d9153a061f3cc05ee11d23859f00622f761229901febdca8fc3eda0 |
|
MD5 | c80b8cd0b1ea1bc543ac249138b2ef2e |
|
BLAKE2b-256 | 2648e7c3ba483b259f08ae7581409aee4cc88ef999b8d08e3f7ea3fe3b7ca083 |