Log without the setup via a pre-configured structlog logger with optional Sentry integration
Project description
Structlog-Sentry-Logger
Documentation: https://structlog-sentry-logger.readthedocs.io
Source Code: https://github.com/TeoZosa/structlog-sentry-logger
:teacher: Overview
A multi-purpose, pre-configured,
performance-optimized structlog
logger with
(optional) Sentry integration
via structlog-sentry
.
:sparkles: Features
- Makes logging as easy as using print statements, but prettier and easier to capture and filter!
- Highly opinionated! There are only two (2) distinct configurations.
- Structured logs in JSON format means they are ready to be ingested by many of your favorite log analysis tools!
- Cloud Logging compatible!
:confetti_ball: What You Get
:muscle: Powerful Automatic Context Fields
The pre-configured options include:
- :watch: Timestamps
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
- :vertical_traffic_light: Log levels
- Added to the JSON context for filtering and categorization
- :mag: Logger names
- Automatically assigned to namespaced versions of the initializing python
modules (
.py
files), relative to your project directory.- e.g., the logger in
docs_src/sentry_integration.py
is nameddocs_src.sentry_integration
- e.g., the logger in
- Automatically assigned to namespaced versions of the initializing python
modules (
With fields sorted by key for easier at-a-glance analysis.
:zap: Performance
structlog-sentry-logger
is C-compiled and fully-tuned,
leveraging orjson
as the JSON serializer for lightning-fast logging (more than a 4x speedup over Python's
built-in JSON library[^1]; see
here
for sample performance benchmarks). Don't let your obligate cross-cutting concerns
cripple performance any longer!
For further reference, see:
- "
orjson
: Serialize" for benchmarks - "
structlog
: Performance" for salient performance-related configurations.
[^1]: Source: Choosing a faster JSON library for Python: Benchmarking
:robot: Built-in Sentry Integration (Optional)
Automatically add much richer context to your Sentry reports.
- Your entire logging context is sent as a Sentry event when
the
structlog-sentry-logger
log level iserror
or higher.- i.e.,
logger.error("")
,logger.exception("")
- i.e.,
- See
structlog-sentry
for more details.
Table of Contents
- :tada: Installation
- :rocket: Usage
- :chart_with_downwards_trend: Output: Formatting & Storage
- :wrench: Development
- :clipboard: Summary
- :books: Further Reading
- :judge: Legal
:tada: Installation
pip install structlog-sentry-logger
:rocket: Usage
:loud_sound: Pure structlog
Logging (Without Sentry)
At the top of your Python module, import and instantiate the logger:
import structlog_sentry_logger
LOGGER = structlog_sentry_logger.get_logger()
Now anytime you want to print anything, don't. Instead do this:
LOG_MSG = "Information that's useful for future me and others"
LOGGER.info(LOG_MSG, extra_field="extra_value")
:memo: Note
All the regular Python logging levels are supported.
Which automatically produces this:
{
"event": "Information that's useful for future me and others",
"extra_field": "extra_value",
"level": "info",
"logger": "docs_src.pure_structlog_logging_without_sentry",
"sentry": "skipped",
"timestamp": "2020-10-18 15:30:05"
}
:goal_net: Sentry Integration
Export your Sentry DSN into your local environment.
- An easy way to do this is to put it into a local
.env
file[^2]:
# On the command line:
SENTRY_DSN=YOUR_SENTRY_DSN
echo "SENTRY_DSN=${SENTRY_DSN}" >> .env
Then load the .env
file in your Python code prior to instantiating the logger, e.g.:
import structlog_sentry_logger
LOGGER = structlog_sentry_logger.get_logger()
:outbox_tray: Log Custom Context Directly to Sentry
With structlog
, you can even incorporate custom messages in your exception handling
which will automatically be reported to Sentry (thanks to the structlog-sentry
module):
import uuid
import structlog_sentry_logger
LOGGER = structlog_sentry_logger.get_logger()
curr_user_logger = LOGGER.bind(uuid=uuid.uuid4().hex) # LOGGER instance with bound UUID
try:
curr_user_logger.warn("A dummy error for testing purposes is about to be thrown!")
x = 1 / 0
except ZeroDivisionError as err:
ERR_MSG = (
"I threw an error on purpose for this example!\n"
"Now throwing another that explicitly chains from that one!"
)
curr_user_logger.exception(ERR_MSG)
raise RuntimeError(ERR_MSG) from err
{
"event": "A dummy error for testing purposes is about to be thrown!",
"level": "warning",
"logger": "docs_src.sentry_integration",
"sentry": "skipped",
"timestamp": "2020-10-18 15:29:55",
"uuid": "181e0e00b9034732af4fed2b8424fb11"
}
{
"event": "I threw an error on purpose for this example!\nNow throwing another that explicitly chains from that one!",
"exception": 'Traceback (most recent call last):\n File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>\n x = 1 / 0\nZeroDivisionError: division by zero',
"level": "error",
"logger": "docs_src.sentry_integration",
"sentry": "sent",
"sentry_id": null,
"timestamp": "2020-10-18 15:29:55",
"uuid": "181e0e00b9034732af4fed2b8424fb11"
}
Traceback (most recent call last):
File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 10, in <module>
x = 1 / 0
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/structlog-sentry-logger/docs_src/sentry_integration.py", line 17, in <module>
raise RuntimeError(ERR_MSG) from err
RuntimeError: I threw an error on purpose for this example!
Now throwing another that explicitly chains from that one!
:cloud: Cloud Logging Compatibility
The logger will attempt to infer if an application is running in a cloud environment by
inspecting for the presence of environment variables that may be automatically injected
by cloud providers (namely, KUBERNETES_SERVICE_HOST
, GCP_PROJECT
,
and GOOGLE_CLOUD_PROJECT
).
If any of these environment variables are detected, log levels will be duplicated to a
reserved severity
key in the emitted logs to enable parsing of the log level and the
remaining log context (as jsonPayload
) by Cloud Logging
(see: Cloud Logging: Structured logging).
:memo: ️Note
This behavior can also be manually enabled by adding theSTRUCTLOG_SENTRY_LOGGER_CLOUD_LOGGING_COMPATIBILITY_MODE_ON
variable to your environment, e.g., via a.env
file[^2].
:warning:️ Warning
If a user manually specifies a value for theseverity
key, it will be overwritten! Avoid using this key if possible to preempt any future issues.
:chart_with_downwards_trend: Output: Formatting & Storage
The default behavior is to stream JSON logs directly to the standard output stream like a proper 12 Factor App.
For local development, it often helps to prettify logging to stdout and save JSON logs
to a .logs
folder at the root of your project directory for later debugging. To enable
this behavior, set the following environment variable
(assuming you are populating environment variables via a .env
file[^2], as in
the Sentry Integration section):
echo "STRUCTLOG_SENTRY_LOGGER_LOCAL_DEVELOPMENT_LOGGING_MODE_ON=" >> .env
In doing so, with our previous exception handling example we would get:
[^2]: This library uses python-dotenv
to automatically populate your environment with this variable (if it exists)
:wrench: Development
For convenience, implementation details of the below processes are abstracted away and encapsulated in single Make targets.
:fire: Tip
Invokingmake
without any arguments will display auto-generated documentation on available commands.
:building_construction: Package and Dependencies Installation
Make sure you have Python 3.7+ and poetry
installed and configured.
To install the package and all dev dependencies, run:
make provision-environment
:fire: Tip
Invoking the above withoutpoetry
installed will emit a helpful error message letting you know how you can install poetry.
:package: Python Module to C-Extension Compilation
The projects's build.py file specifies which modules to package.
For manual per-module compilation, see: Mypyc Documentation: Getting started - Compiling and running
:white_check_mark: Testing
We use tox
and pytest
for our test automation and testing
frameworks, respectively.
To invoke the tests, run:
make test
Run mutation tests to validate test suite robustness (Optional):
make test-mutations
:information_source: Technical Details
Test time scales with the complexity of the codebase. Results are cached in.mutmut-cache
, so once you get past the initial cold start problem, subsequent mutation test runs will be much faster; new mutations will only be applied to modified code paths.
:rotating_light: Code Quality
We use pre-commit
for our static analysis automation and
management framework.
To invoke the analyses and auto-formatting over all version-controlled files, run:
make lint
:rotating_light: Danger
CI will fail if either testing or code quality fail, so it is recommended to automatically run the above locally prior to every commit that is pushed.
:arrows_counterclockwise: Automate via Git Pre-Commit Hooks
To automatically run code quality validation on every commit (over to-be-committed files), run:
make install-pre-commit-hooks
:warning:️ Warning
This will prevent commits if any single pre-commit hook fails (unless it is allowed to fail) or a file is modified by an auto-formatting job; in the latter case, you may simply repeat the commit and it should pass.
:memo: Documentation
make docs-clean docs-html
:fire: Tip
For faster feedback loops, this will attempt to automatically open the newly built documentation static HTML in your browser.
:clipboard: Summary
That's it. Now no excuses. Get out there and program with pride knowing no one will laugh at you in production! For not logging properly, that is. You're on your own for that other observability stuff.
:books: Further Reading
:one: structlog
: Structured Logging for Python
:two: Sentry
: Monitor and fix crashes in realtime
:three: structlog-sentry
: Provides the structlog
integration for Sentry
:judge: Legal
:page_facing_up: License
Structlog-Sentry-Logger is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
:busts_in_silhouette: Credits
This project was generated from
@TeoZosa
's
cookiecutter-cruft-poetry-tox-pre-commit-ci-cd
template.
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 Distributions
File details
Details for the file structlog-sentry-logger-0.14.1.tar.gz
.
File metadata
- Download URL: structlog-sentry-logger-0.14.1.tar.gz
- Upload date:
- Size: 27.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddcb4ac1b16573d6c74af24bc3469bae095e55a87b5d300294e5d11857960c57 |
|
MD5 | 7ff3bf3070864e9bcbddaa17f7bf6626 |
|
BLAKE2b-256 | cf64f49812e2f90e15f2ddf340de44828559f7b8587f47b09b16acc2caca7bca |
File details
Details for the file structlog_sentry_logger-0.14.1-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 86.2 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42adbd6726f956508f2972e03a1ce327bc9da72dcc10bde76b1883f53282420d |
|
MD5 | 6fa452afb4a7447f0fe8c969b9f849e9 |
|
BLAKE2b-256 | 86d3ed6a0d0130968b694679b4fcc713f4120d9c641efa468103a548823b5340 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp39-cp39-manylinux_2_31_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp39-cp39-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 332.2 kB
- Tags: CPython 3.9, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fc058cc2d9d3989a1cfe64a3a51acb0366eac91e7709f88b6a6796bf3163a0d |
|
MD5 | 515a08dcca43bb7ecdcc25d9069d5ebf |
|
BLAKE2b-256 | 2bcdceb749ebcb052174db849b642afa8581ba91ba393b3cd7422bd7deb16d11 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp39-cp39-macosx_10_16_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp39-cp39-macosx_10_16_x86_64.whl
- Upload date:
- Size: 90.2 kB
- Tags: CPython 3.9, macOS 10.16+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2e95194eab5260deb0bb49a3f0ae670044d4a9d4742b504cc13fcf442a5d120 |
|
MD5 | 3efe803dbac271b49f9d06ec03793fc7 |
|
BLAKE2b-256 | 6abb7f7ecd86741b6fe079ecd91cc2ce3d559d341256f01a5916f0f57736c6d2 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 85.3 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20daacfdab10644575d0ad0728b713bab4f63dcf9960eae21e40c70de331017a |
|
MD5 | 329f1114a75fe0635a0e39f03b5cf8b8 |
|
BLAKE2b-256 | 1ec3f24ca8bc06dbc1d1bbf0ea26a913dcc8e54f15111cc298d03a32dfb920cb |
File details
Details for the file structlog_sentry_logger-0.14.1-cp38-cp38-manylinux_2_31_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp38-cp38-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 324.5 kB
- Tags: CPython 3.8, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fab9512e24448f901d00c9b3cd1e9dcd0a0224cce3f439d07e71fb6c0c1e195 |
|
MD5 | 4084b093362b33e16cc79335aa3ef515 |
|
BLAKE2b-256 | d7affba4a7534462280f5b7c4c183dbabaa78025d969029b2aa8de16506f2c6d |
File details
Details for the file structlog_sentry_logger-0.14.1-cp38-cp38-macosx_10_16_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp38-cp38-macosx_10_16_x86_64.whl
- Upload date:
- Size: 89.1 kB
- Tags: CPython 3.8, macOS 10.16+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f131ff14f54a55c82e2deb6dc747df51f446824971fb2a535cdde1d5bbed9274 |
|
MD5 | c86963000903b1482ad39a064eee3eaa |
|
BLAKE2b-256 | 518091c92b9cae269185ee00a6bc94782857664e064bebec09c4adc54049f827 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 85.2 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 326db3451045c477bf04674085e5b88091feb7a9bcf17b94689143514734e3bf |
|
MD5 | 2bbaf3d1969a379d340aa10916fb404d |
|
BLAKE2b-256 | 622a496d8e8adfeb39ee324d802c612891810049bc0f5d6c5b0cea7d148ddc87 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp37-cp37m-manylinux_2_31_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp37-cp37m-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 301.5 kB
- Tags: CPython 3.7m, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f661e8cb759aa93583b8792443454b74231f2343cda75553474b789d616410e |
|
MD5 | b54d014ada96b80807731091f477fef4 |
|
BLAKE2b-256 | bf271bc3f4571f82da2ca29547007d163ee314a704d81daa3165a6a131c2e7a2 |
File details
Details for the file structlog_sentry_logger-0.14.1-cp37-cp37m-macosx_10_16_x86_64.whl
.
File metadata
- Download URL: structlog_sentry_logger-0.14.1-cp37-cp37m-macosx_10_16_x86_64.whl
- Upload date:
- Size: 88.1 kB
- Tags: CPython 3.7m, macOS 10.16+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad4fc72047cf4fa0cf5757d64466b63ff51b59268a16f7cec553a5b5c9408dac |
|
MD5 | a04a29cc43544f7bcf5a2d03a52a49a3 |
|
BLAKE2b-256 | 392b5466648ec7c8aea9d5af431efc9dbee075288b834487569793d73f3dfbc1 |