Skip to main content

A logger that automagically wraps Structlog with Sentry integration (via `structlog-sentry`) with a set of multi-purpose pre-configured options.

Project description

Structlog-Sentry-Logger

A logger that automagically wraps Structlog with Sentry integration via structlog-sentry with a set of multi-purpose pre-configured options.

Benefits:
  1. Makes logging as easy as using print statements, but prettier and less smelly!
  2. Highly opinionated! There is literally nothing you are allowed to configure!
  3. 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!

Usage

Out-of-the-box

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")

Sentry Integration

Export your Sentry DSN into a local .env file

   CI_ENVIRONMENT_SLUG=dev-local
SENTRY_DSN=YOUR_SENTRY_DSN
echo "SENTRY_DSN=${SENTRY_DSN}" > .env
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",
}

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!

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


Download files

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

Source Distribution

structlog-sentry-logger-0.2.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

structlog_sentry_logger-0.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file structlog-sentry-logger-0.2.0.tar.gz.

File metadata

  • Download URL: structlog-sentry-logger-0.2.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Darwin/19.6.0

File hashes

Hashes for structlog-sentry-logger-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c27c799ce7fb4faaada129132812e5950894c173ebb44023a4bc81c6789adefa
MD5 c266a23b3c4b14e6f428072592884ee1
BLAKE2b-256 5f6f9c7bfecba5713fa6ab70bd1ddc0d655f8dc4800d1ee64e06083a7f780c3e

See more details on using hashes here.

File details

Details for the file structlog_sentry_logger-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for structlog_sentry_logger-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cad27e50357a6b05dcd212790fff11ae3916c2701870d185dc2b9d9258c947a
MD5 e1c6bc90f180642c04ccbd7654123e5b
BLAKE2b-256 0126e4d773ee64bc41eda1cf1872d951a6a961ca385e2cdfd75058d6cba04aef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page