#Sentry.logger import error

1 messages · Page 1 of 1 (latest)

steel gulch
#

sentry states in its docs that i should use sentry.logger to use the logger, however it gives me an error stating that it is not a valid module (logger). Im using python sdk

spare steepleBOT
#

Hi @steel gulch. I'm happy to dig into this for you! I'll check my internal sources for answers to your question! :wave:

#

The correct way to use Sentry's logger in the Python SDK is to import it from the SDK as follows:

from sentry_sdk import logger as sentry_logger

You should not use import sentry.logger or import sentry_sdk.logger as a module, since logger is not a standalone module but an attribute of the sentry_sdk package. Attempting to import it as a module will result in an import error.

Here's an example of the correct usage:

from sentry_sdk import logger as sentry_logger

sentry_logger.info('Updated global cache')
sentry_logger.error('Failed to process payment. Order: {order_id}. Amount: {amount}', order_id="or_2342", amount=99.99)

Make sure you have initialized the SDK with the experimental logs feature enabled:

import sentry_sdk

sentry_sdk.init(
    dsn="<https://[email protected]/0",>
    _experiments={
        "enable_logs": True,
    },
)

If you follow this pattern and are using Sentry Python SDK version 2.28.0 or above, it should work as expected. If you are still seeing errors, please ensure your SDK version is up to date and that you are not trying to import logger as a module. For more details, see the official Sentry documentation on Python logs setup.

#

If you are following the documentation and still see an error, double-check that you are not using an outdated SDK version or referencing the wrong import path. The knowledge sources do not mention any valid usage of import sentry.logger or import sentry_sdk.logger as a module, only as an attribute of sentry_sdk after proper initialization with _experiments={"enable_logs": True} enabled Set Up Logs.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: