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.35.0 and above.
pip install "sentry-sdk"
To enable logging, you need to initialize the SDK with the enable_logs option set to True.
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
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, warning, error, and fatal.
You can send structured messages by using the {attribute_name} placeholder syntax. The properties of this message 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.warning('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")
You can also pass additional attributes directly to the logging functions via the attributes kwarg.
from sentry_sdk import logger as sentry_logger
sentry_logger.error(
'Payment processing failed',
attributes={
'payment.provider': 'stripe',
'payment.method': 'credit_card',
'payment.currency': 'USD',
'user.subscription_tier': 'premium'
}
)
To filter logs, or update them before they are sent to Sentry, you can use the before_send_log option.
import sentry_sdk
from sentry_sdk.types import Log, Hint
from typing import Optional
def before_log(log: Log, _hint: Hint) -> Optional[Log]:
# Filter out all info level logs
if log["severity_text"] == "info":
return None
return log
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
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 dict has the following keys:
severity_text: (str- one oftrace,debug,info,warning,error,fatal) The log level.severity_number: (int) The log level as a number ranging from 1 to 24, as per the OpenTelemetry specification ofSeverityNumber.body: (str) The log message.attributes: (dict[str, str | bool | float | int]) Additional attributes to be sent with the log.time_unix_nano: (int) The timestamp of the log in nanoseconds since the Unix epoch.trace_id: (Optional[str]) The trace ID of the trace this log belongs to.
The Python SDK automatically sets several default attributes on all log entries to provide context and improve debugging:
environment: The environment set in the SDK if defined. This is sent from the SDK assentry.environment.release: The release set in the SDK if defined. This is sent from the SDK assentry.release.sdk.name: The name of the SDK that sent the log. This is sent from the SDK assentry.sdk.name.sdk.version: The version of the SDK that sent the log. This is sent from the SDK assentry.sdk.version.
If the log was parameterized, Sentry adds the message template and parameters as log attributes.
message.template: The parameterized template string. This is sent from the SDK assentry.message.template.message.parameter.X: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (sentry.message.parameter.0,sentry.message.parameter.1, etc) or the parameter's name (sentry.message.parameter.item_id,sentry.message.parameter.user_id, etc). This is sent from the SDK assentry.message.parameter.X.
server.address: The address of the server that sent the log. Equivalent toserver_namethat gets attached to Sentry errors.
If user information is available in the current scope, the following attributes are added to the log:
user.id: The user ID.user.name: The username.user.email: The email address.
If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.
origin: The origin of the log. This is sent from the SDK assentry.origin.
Available integrations:
If there's an integration you would like to see, open a new issue on GitHub.
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").