Better Stack Python logging

Start logging in 3 steps

Where do you want to collect your logs from?

Logging from Django

Collect logs from your Django application.

1. Install

Install Better Stack Python PyPI package:

Install Logtail Python
pip3 install logtail-python

2. Setup

Set up Django Better Stack handler in settings.py:

Set up Logtail handler
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    'handlers': {
        'logtail': {
            'class': 'logtail.LogtailHandler',
            'source_token': "$SOURCE_TOKEN",
        },
    },
    "loggers": {
        "": {
            "handlers": [
                "logtail",
            ],
            "level": "INFO",
        },
    },
}

3. Start logging πŸŽ‰

Use Python logger as usual:

Send logs to Logtail
import logging
logger = logging.getLogger(__name__)
logger.error('Something bad happened.')
logger.info('Log message with structured logging.', extra={
    'item': "Orange Soda",
    'price': 100.00
})

You should see your logs in Better Stack β†’ Live tail.

Python version 3.7 or higher is required.
Pip version 20.0.2 or higher is required.

Logging from Python

Collect logs from your Python code.

1. Install

Install Better Stack Python PyPI package:

Install Logtail Python
pip3 install logtail-python

2. Setup

Set up Python logger with Better Stack:

Set up Logtail handler
from logtail import LogtailHandler
import logging

handler = LogtailHandler(source_token="$SOURCE_TOKEN")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.handlers = []
logger.addHandler(handler)

3. Start logging πŸŽ‰

Use Python logger as usual:

Send logs to Logtail
logger.error('Something bad happened.')
logger.info('Log message with structured logging.', extra={
    'item': "Orange Soda",
    'price': 100.00
})

You should see your logs in Better Stack β†’ Live tail.

Python version 3.7 or higher is required.
Pip version 20.0.2 or higher is required.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! πŸ™

Additional information

Interested in learning more about Python log levels and using formatters to customize your logs? Check out our Comprehensive Guide to Logging in Python.

Missing logs from the end of your application run?

Make sure your application terminates gracefully, giving the logger enough time to finish the request to Better Stack.

Use sys.exit() to end your application.

Using os._exit() would terminate all threads immediately, potentially dropping your logs.

Enrich log context

Add custom data to multiple log lines using with block:

Enrich logs with context

with logtail.context(user={ 'id': 123 }):
    logger.info('User logged in.')
    # More code here ...
    logger.info('User bought a python.')

Log exceptions

Use logger.exception() to log exceptions with traceback:

Log exception traceback
try:
    nonexisting_function()
except Exception as Argument:
    logger.exception("Error occurred while calling non-existing function.")

Example project

Want to try a more detailed example?
See our Python Logtail example project on GitHub.