Last active
April 5, 2018 08:31
-
-
Save devlights/35e78b590f9b837a9f8ba5bc4feb952c to your computer and use it in GitHub Desktop.
[python][sentry] Sentryとloggingモジュールの組み合わせサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Sentry と logging の組み合わせのサンプル | |
DSN は 環境変数 SENTRY_DSN に設定してから起動します。 | |
(*) Windowsの場合、一時設定するには以下のようにします。 | |
$ set SENTRY_DSN=xxxxx | |
REFERENCES:: | |
https://docs.python.jp/3/library/logging.config.html#logging-config-dictschema | |
https://docs.sentry.io/clients/python/integrations/logging/ | |
""" | |
import logging.config | |
from raven import Client | |
from raven.conf import setup_logging | |
from raven.handlers.logging import SentryHandler | |
handler = SentryHandler(Client()) | |
setup_logging(handler) | |
logger = logging.getLogger(__name__) | |
def go(): | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warning('warning message') | |
try: | |
1 / 0 | |
except ZeroDivisionError: | |
logger.error('error message', exc_info=True) | |
if __name__ == '__main__': | |
go() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Sentry と logging の組み合わせのサンプル | |
DSN は 環境変数 SENTRY_DSN に設定してから起動します。 | |
ログレベルがDEBUGやINFOのものもSentryに送信するには | |
以下のように logging 側の level と SentryHandler 側の level を調整します。 | |
(*) Windowsの場合、一時設定するには以下のようにします。 | |
$ set SENTRY_DSN=xxxxx | |
REFERENCES:: | |
https://docs.python.jp/3/library/logging.config.html#logging-config-dictschema | |
https://docs.sentry.io/clients/python/integrations/logging/ | |
""" | |
import logging.config | |
from raven import Client | |
from raven.conf import setup_logging | |
from raven.handlers.logging import SentryHandler | |
logging.basicConfig(level=logging.DEBUG) | |
client = Client() | |
handler = SentryHandler(client, level=logging.DEBUG) | |
setup_logging(handler) | |
logger = logging.getLogger(__name__) | |
def go(): | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warning('warning message') | |
try: | |
1 / 0 | |
except ZeroDivisionError: | |
logger.error('error message', exc_info=True) | |
if __name__ == '__main__': | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bashの場合は以下のように設定
$ export SENTRY_DSN=xxxxx