Log without the setup via a pre-configured structlog logger with optional Sentry integration
Project description
Structlog-Sentry-Logger
A multi-purpose, pre-configured structlog
logger
with (optional) Sentry integration
via structlog-sentry
.
Benefits:
- Makes logging as easy as using print statements, but prettier and less smelly!
- Highly opinionated! There are only two (2) distinct configurations.
- Structured logs in JSON format means they are ready to be ingested by many favorite log analysis tools!
Save your tears for what you do best: writing fancy computer mumbo jumbo to make the world a better place!
Table of Contents
Usage
Pure structlog
Logging (Without Sentry)
At the top of your Python module, import and instantiate the logger:
from structlog_sentry_logger import logger
LOGGER = logger.get_logger()
Now anytime you want to print anything, don't. Instead do this:
LOGGER.info("Information that's useful for future me and others", extra_field="extra_value")
Note: all the regular Python logging levels are supported.
{
"event": "Information that's useful for future me and others",
"extra_field": "extra_value",
"level": "info",
"logger": "<input>",
"timestamp": "2020-09-25 17:21:26",
}
With structlog
, you can even incorporate custom messages in your exception handling:
import uuid
from structlog_sentry_logger import logger
LOGGER = 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!")
assert False
except AssertionError 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": "<input>",
"timestamp": "2020-09-25 17:19:02",
"uuid": "68f595440e69478a97a26b002f9cbf44",
}
{
"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 "<input>", line 8, in <module>\nAssertionError',
"level": "error",
"logger": "<input>",
"timestamp": "2020-09-25 17:19:02",
"uuid": "68f595440e69478a97a26b002f9cbf44",
}
Traceback (most recent call last):
File "<input>", line 8, in <module>
AssertionError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 13, in <module>
RuntimeError: I threw an error on purpose for this example!
Now throwing another that explicitly chains from that one!
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 and usepython-dotenv
to populate your environment:
# On the command line:
SENTRY_DSN=YOUR_SENTRY_DSN
echo "SENTRY_DSN=${SENTRY_DSN}" > .env
# In your Python code, prior to instantiating the logger:
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
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. To enable
this behavior, set the following environment variable:
CI_ENVIRONMENT_SLUG=dev-local
In doing so, with our previous exception handling example we would get:
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.
Further Reading
structlog
: Structured Logging for Python
Sentry
: Monitor and fix crashes in realtime.
structlog-sentry
: Provides the structlog
SentryProcessor
for Sentry
integration.
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
Hashes for structlog-sentry-logger-0.3.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76c8bc5efd98984589fb11e32213c0507d008945ebed91af82313559ad7729f7 |
|
MD5 | 4b857e50749720f3d347d89f30be4ca6 |
|
BLAKE2b-256 | 83e600748e26762e6cdc1ce535f49fda2895d2c332e03f619a2c198e725aad2c |
Hashes for structlog_sentry_logger-0.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2795ec0fdf186b579b1af3cac0eccd8a1598f791faeb51c6d32e4b4b4a840c68 |
|
MD5 | 74c21c6a7ec4c3ab7b1cbd0e4ab19337 |
|
BLAKE2b-256 | 765f65b91b1f90528874b3da7f6aa3fbf5d9cb1306f09978d9c93062b3b31f56 |