Set Up Logs
Structured logs allow you to send, view and query logs sent from your applications within Sentry.
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
Logs for Python are supported in Sentry Python SDK version 2.27.0
and above.
pip install --upgrade sentry-sdk
To enable logging, you need to initialize the SDK with the _experiments.enable_logs
option set to true
.
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
_experiments={
"enable_logs": True,
},
)
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the sentry_sdk.logger
APIs.
The logger
namespace exposes six methods that you can use to log messages at different log levels: trace
, debug
, info
, warn
, error
, and fatal
.
You can pass additional attributes directly to the logging functions. These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
from sentry_sdk import logger as sentry_logger
sentry_logger.trace('Starting database connection {database}', database="users");
sentry_logger.debug('Cache miss for user {user_id}', user_id=123);
sentry_logger.info('Updated global cache');
sentry_logger.warn('Rate limit reached for endpoint {endpoint}', endpoint='/api/results/');
sentry_logger.error('Failed to process payment. Order: {order_id}. Amount: {amount}', order_id="or_2342", amount=99.99);
sentry_logger.fatal('Database {database} connection pool exhausted', database="users");
Sentry automatically instruments Python loggers via it's LoggingIntegration
.
import logging
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
_experiments={
"enable_logs": True
}
)
# Your existing logging setup
my_logger = logging.Logger("some-logger")
my_logger.debug('In this example debug events will not be sent to Sentry logs. my_value=%s', my_value)
my_logger.info('But info events will be sent to Sentry logs. my_value=%s', my_value)
The logging integration automatically monkeypatches a handler into all loggers except for the root logger. If you'd like to manually configure the handler, you can do that like so:
import sentry_sdk
from sentry_sdk.integrations.logging import SentryLogsHandler
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
dsn="...",
_experiments={
"enable_logs": True
},
integrations=[
LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler
]
}
# Instead, configure the root logger to send INFO-level logs to Sentry
logging.basicConfig(level=logging.INFO, handlers=[SentryLogsHandler(level=logging.INFO)])
To filter logs, or update them before they are sent to Sentry, you can use the _experiments.before_send_log
option.
def _before_log(log):
if log.level == "info":
# Filter out all info logs
return None
return log
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
_experiments={
"enable_logs": True,
"before_send_log": _before_log,
},
)
The before_send_log
function receives a log object, and should return the log object if you want it to be sent to Sentry, or None
if you want to discard it.
The log object has the following properties:
level
: (string - one oftrace
,debug
,info
,warn
,error
,fatal
) The log level.message
: (string) The message to be logged.timestamp
: (number) The timestamp of the log.attributes
: (object) The attributes of the log.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").