Get started with logging from your Python code using logfire-python and send your logs to logfire.

Logging from Python

1. Install

Install Logfire Python PyPI package:

Install Logfire Python

pip3 install logfire-python

2. Set up Python logger with Logfire:

from logfire import LogfireHandler import logging
handler = LogfireHandler(source_token=$SOURCE_TOKEN)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.handlers = []
logger.addHandler(handler)

Replace $SOURCE_TOKEN with your source token. You can get your source tokens in your Logfire sources.

3. Start logging 🎉

logger.error('An Error occured.')
logger.info('Structured logging message.', extra={
    'item': "Superbike",
    'color': "pink"
})

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

Logging from Django

Collect logs from your Django application.

1. Install

Install Logfire Python PyPI package:

Install Logfire Python

pip3 install logfire-python

2. Setup

Set up Django Better Stack handler in settings.py:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    'handlers': {
        'logfire': {
            'class': 'logfire.LogfireHandler',
            'source_token': "$SOURCE_TOKEN",
        },
    },
    "loggers": {
        "": {
            "handlers": [
                "logfire",
            ],
            "level": "INFO",
        },
    },
}

Insert your source token

Replace $SOURCE_TOKEN with your source token. You can get your source tokens in your Logfire sources.

3. Start logging 🎉

Use Python logger as usual:

import logging
logger = logging.getLogger(__name__)
logger.error('An Error occured!!!')
logger.info('Structured logging message.', extra={
    'context_object': "Superbike",
    'color': "pink"
})

You should see your logs in Logfire → Live tail.