Skip to main content

library for scoped logging on top of structlog (or similar sinks)

Project description

linux test status

scopedlog

A library for scoped logging on top of structlog (or similar sinks), inspired by the idea of an apparently defunct lithoxyl. Note that I (kshpytsya) have deliberatly not looked into actual lithoxyl's implementation to keep a fresh mind.

Here is an example:

import contextlib                                                                                                                         import structlog
import structlog.stdlib
import structlog.dev
import structlog.processors
import scopedlog
import scopedlog.sink_structlog
import scopedlog.idgen_seq

structlog.configure(processors=[
    structlog.stdlib.add_log_level,
    structlog.processors.ExceptionPrettyPrinter(),
    structlog.dev.ConsoleRenderer()
])

slog = scopedlog.ScopedLog(
    sink=scopedlog.sink_structlog.StructlogSink(structlog.get_logger()),
    id_gen=scopedlog.idgen_seq.GlobalSeqScopeIdGenenerator()
)

with contextlib.suppress(RuntimeError):
    with slog.info("scope1", k1=1) as sl:
        sl.debug("state1", k2=2, k3=3)
        sl.info("state1", k4=4)
        sl.warn("state1", k5=5)
        raise RuntimeError("bad one")

with contextlib.suppress(RuntimeError):
    with slog.debug("scope2", k1=1) as sl:
        sl.debug("state1", k2=2, k3=3)
        sl.info("state1", k4=4)
        sl.warn("state1", k5=5)
        raise RuntimeError("bad too ;)")

with slog.debug("scope3") as sl:
    sl.fail()

with slog.debug("scope4") as sl:
    sl.exit_kw["k1"] = 1

And expected output:

See tests for more examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for scopedlog, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size scopedlog-0.1.4.tar.gz (7.1 kB) File type Source Python version None Upload date Hashes View
Filename, size scopedlog-0.1.4-py3-none-any.whl (5.8 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page