Created
July 12, 2012 14:30
-
-
Save bbigras/3098463 to your computer and use it in GitHub Desktop.
template pour Raven
This file contains 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
# -*- coding: utf-8 -*- | |
from raven import Client | |
from raven.handlers.logging import SentryHandler | |
from raven.conf import setup_logging | |
import sys | |
import logging | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
logger = logging.getLogger() # .getLogger(__name__) dans les sous-modules | |
logger.setLevel(logging.DEBUG) | |
# Sentry | |
clientRaven = Client(dsn='http://urlsentry_avec_id_et_cles') | |
handler = SentryHandler(clientRaven) | |
handler.setLevel(logging.WARN) | |
setup_logging(handler) | |
# Console | |
consoleLogger = logging.StreamHandler() | |
consoleLogger.setLevel(logging.DEBUG) | |
consoleLogger.setFormatter(formatter) | |
logger.addHandler(consoleLogger) | |
# Fichier | |
# fileLogger = logging.FileHandler(filename='errors.log') | |
# fileLogger.setLevel(logging.ERROR) | |
# fileLogger.setFormatter(formatter) | |
# logger.addHandler(fileLogger) | |
def register_handler(orig_excepthook=sys.excepthook): | |
def error_catcher(*exc_info): | |
if exc_info[0] is KeyboardInterrupt: | |
logger.info("Fermeture de l'application avec un KeyboardInterrupt") | |
raise SystemExit() | |
else: | |
logger.critical("Unhandled exception", exc_info=exc_info) | |
orig_excepthook(*exc_info) | |
sys.excepthook = error_catcher | |
# « catch » les exceptions « non-catcheés » | |
register_handler() | |
# demo | |
try: | |
1 / 0 | |
except Exception, e: | |
logger.exception(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment