No stdout logging when initializing sentry_sdk in Kubernetes pod

We want to enable Sentry error forwarding for our Kubernetes Deployments. In the __init__.py of the main package folder so root/package/__init__.py we set up Sentry like this:

import logging

from os import environ

import sentry_sdk

logging.info("initializing sentry")
sentry_sdk.init(
    release=environ.get("SENTRY_RELEASE"), environment=environ.get("SENTRY_ENVIRONMENT")
)

This works just fine, it correctly pushes errors to Sentry for us to examine but one large issue is that it seems to completely eat any logs we write using logging.info(...) in any other location, so nothing is written to stdout. This is a problem, as we frequently examine logging trails in Stackdriver for debugging and monitoring.

We set up a logging config in an internal python package like this:

logging.basicConfig(
    format="%(asctime)s - %(module)s.py:%(funcName)s:%(lineno)s - %(levelname)s - %(message)s",
    level=logging.INFO,
)

Any suggestions?

Thanks a lot in advance! Please let me know if you need more info.

We found a solution, which is slightly confusing but pretty simple:

Add the logging configuration above sentry_sdk.init(...):

## this
logging.basicConfig(
    format="%(asctime)s - %(module)s.py:%(funcName)s:%(lineno)s - %(levelname)s - %(message)s",
    level=logging.INFO,
)
##
logging.info("initializing sentry")
sentry_sdk.init(
    release=environ.get("SENTRY_RELEASE"), environment=environ.get("SENTRY_ENVIRONMENT"), traces_sample_rate=1.0,
)

logging.info("This works normally now!")

It seems to me that you just need to initialize your logging config before you start logging anything. I’m not sure if that’s SDK-related at all. Does the issue actually stop happening if you comment out the SDK init without moving around the logging config?

Yes, logging works normally if we don’t enable Sentry at all. It was initialized in a different module. Not sure why behaviour works this way.