python project integrated sentry reported

log in Register

Login Sentry official website ( https://sentry.io/) registered account

Create a project

Create a test project sentry
Select the language project

In the demo you can get your own DSN

Capture trigger abnormal report

Configure the local client

Firstly pip install Sentry SDK

pip install raven --upgrade 

Then initialize the client:

from raven import Client

DSN = 'https://****@sentry.io/****'
client = Client(DSN)

Finally, call client.captureException () in the code where we need to record abnormalities.

try:
    1 / 0
except ZeroDivisionError:
    client.captureException()

test program

Many times our exception handling should contain more contextual information, we do so for subsequent analysis will be more help, then we can join them before the exception context information captured in sentry.

try:
    processing(user, data)

except:
    client.user_context({
        'user': user.email,
        'data': json.dumps(data)
    })
    client.captureException()

Sentry embedded in the frame

We can not be in every place possible exception code are calling sentry, it is impossible to repair past the sentry code to each implant. A good suggestion is that whenever your program has a unified exception handling mechanism, the best is global. In this case, you simply write Sentry global exception handler.
In addition Sentry popular development framework also provides special support, such Flask, Django, etc. In these applications, as long as you configure the line, you do not need to write what global exception handling (although it is not difficult to write).

Flask of demo

sentry = Sentry(dsn='http://public_key:[email protected]/1')

def create_app():
    app = Flask(__name__)
    sentry.init_app(app)
    return app

Django specific demo

import os
import raven

INSTALLED_APPS = (
    'raven.contrib.django.raven_compat',
)

RAVEN_CONFIG = {
    'dsn': 'http://public_key:[email protected]/1',
    # If you are using git, you can also automatically 
    # configure the release based on the git info.
    'release': raven.fetch_git_sha(os.path.abspath(os.pardir)),
}

The log access module sentry

Access Code Example:

import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration

# All of this is already happening by default!
sentry_logging = LoggingIntegration(
    level=logging.INFO,        # Capture info and above as breadcrumbs
    event_level=logging.ERROR  # Send errors as events
)
sentry_sdk.init(
    dsn="https://[email protected]/1501024",
    integrations=[sentry_logging]
)

logging.debug("I am ignored")
logging.info("I am a breadcrumb")
logging.error("I am an event", extra=dict(bar=43))
logging.error("An exception happened", exc_info=True)

Ignore a logger in multiple logger entire program is not to be reported.

from sentry_sdk.integrations.logging import ignore_logger


ignore_logger("a.spammy.logger")

logger = logging.getLogger("a.spammy.logger")
logger.error("hi")  # no error sent to sentry

The sentry client access logging configuration

from raven.handlers.logging import SentryHandler

logger = logging.getLogger()
logger.addHandler(SentryHandler(level=logging.DEBUG, dsn="https://[email protected]/1501024"))

TODO: There is a problem that even if the order level, only a warning in the form will be reported.

Reprinted link

https://segmentfault.com/a/1190000014847638

Reference links

https://blog.ansheng.me/article/docker-sentry-django-email-dingtalk/

https://docs.sentry.io/platforms/python/logging/

https://github.com/getsentry/sentry-python/issues/228

Guess you like

Origin blog.csdn.net/Enjolras_fuu/article/details/95315260