Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sentry Structured Logging for Python #4220

Discussion options

Note: These instructions are now available in the Sentry docs - https://docs.sentry.io/platforms/python/logs/

Sentry is adding support for structured logs, so you can view and query your logs alongside your traces and errors—all in one place.

Sentry logs screenshot

Note

To get early access to the Sentry logging product and to see how it works, see the announcement post.

To use the new Logging APIs to send application logs directly to Sentry, you'll have to upgrade to 2.25.1 of the Python SDK or higher.

pip install --upgrade sentry-sdk

Logging is gated by an experimental option, _experiments.enable_logs.

sentry_sdk.init(
 dsn=...,
 _experiments={
 "enable_logs": True,
 },
)

Then you can import and use methods from the logger namespace to send logs to Sentry.

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 also automatically instruments Python loggers via the LoggingIntegration:

import logging
sentry_sdk.init(
 dsn="...",
 _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 this:

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.

You must be logged in to vote

Replies: 3 comments 9 replies

Comment options

(削除) Just a note: this is yet experimental and right now is not optimized, so it will make a lot of http requests to Sentry at the moment. So please be careful and do not enable this in your production system yet! :-) (削除ここまで)

Edit: We added batching in 2.25.1 of the Python SDK, and have updated the instructions to reflect this. Please only use logging with Python SDK 2.25.1+

You must be logged in to vote
0 replies
Comment options

We’ve released 2.26.0 of the Python SDK which contains some improvements around logging. We added a variety of new default attributes like server name and sdk name/version which should make debugging easier.

https://github.com/getsentry/sentry-python/releases/tag/2.26.0

Please upgrade and give it a try!

You must be logged in to vote
0 replies
Comment options

@AbhiPrasad @antonpirker Is there a way I can send JSON formatted logs to sentry and use it as parameterized ones?

You must be logged in to vote
9 replies
Comment options

It does not actually fulfill our requirement at the moment

Could you elaborate why this is the case? Are you not able to change what you pass into the logger atm?

Comment options

It does not actually fulfill our requirement at the moment

Could you elaborate why this is the case? Are you not able to change what you pass into the logger atm?

Yeah, we already have pre-configured JSON logs throughout our application, and we want to forward them to Sentry as they are, without modifying them. Making changes would require updates across our codebase, which we’d like to avoid at this point.

Comment options

Understood!

One solution here in the time being is to carry on calling logger.info(json.dumps(log)), but then use the before_send_log SDK option to grab the json serialized log, parse it for the attributes and add them accordingly.

Comment options

@AbhiPrasad Does JSON serialization support exist now, by any chance?

Comment options

Hey @lwpamihiranga - we don't have support for this at the current moment. It's being tracked in this GitHub issue: getsentry/sentry#98042

Are you looking for just the log message to have unfurled json? Or for attributes attached to the log as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /