Explore documentation
Installing Logtail Python client
Logtail allows you to collect logs directly from any Python code, including Django. Supported language versions are:
- Python 3.6.5 or newer
pip
20.0.2 or newer
Installing Logtail python client
Install the Logtail python client library using the pip
command:
pip install logtail-python
How it works in action
Here is an example of how to send logs from your python application to Logtail.
For a more detailed example, please see the Python + Logtail example project on GitHub .
Example: Snippet added to your app
from logtail import LogtailHandler
import logging
handler = LogtailHandler(source_token="<your-source-token>")
logger = logging.getLogger(__name__)
logger.handlers = []
logger.setLevel(logging.INFO)
logger.addHandler(handler)
logger.info('logtail is ready')
logger.warning('log structured data', extra={
'item': {
'url': "https://fictional-store.com/item-123",
'price': 100.00
}
})
Example: JSON rows sent to Logtail
{
"dt": "2021-03-29T11:24:21.788451Z",
"level": "info",
"message": "logtail is ready",
"context": {
"runtime": {
"function": "function_name",
"file": "script_file.py",
"line": 10,
"thread_id": "123456789",
"thread_name": "async_thread",
"logger_name": "logger"
},
"system": {
"pid": 123456,
"process_name": "python"
}
}
}
{
"dt": "2021-03-29T11:24:21.788451Z",
"level": "warning",
"message": "logt structured data",
"item": {
"url": "https://fictional-store.com/item-123",
"price": 100.00
},
"context": {
"runtime": {
"function": "function_name",
"file": "script_file.py",
"line": 11,
"thread_id": "123456789",
"thread_name": "async_thread",
"logger_name": "logger"
},
"system": {
"pid": 123456,
"process_name": "python"
}
}
}