Digitale bierlijst

logger.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os.path
  2. import logging
  3. import logging.config
  4. try:
  5. from raven import Client
  6. from raven.handlers.logging import SentryHandler
  7. from raven.conf import setup_logging
  8. HAVE_SENTRY = True
  9. except ImportError:
  10. HAVE_SENTRY = False
  11. LOG_CONFIG = {
  12. "version": 1,
  13. "disable_existing_loggers": True,
  14. "formatters": {
  15. "console": {
  16. "format": "[%(asctime)s][%(levelname)s] %(name)s "
  17. "%(filename)s:%(funcName)s:%(lineno)d | %(message)s",
  18. "datefmt": "%H:%M:%S",
  19. }
  20. },
  21. "handlers": {
  22. "console": {
  23. "level": "DEBUG",
  24. "class": "logging.StreamHandler",
  25. "formatter": "console",
  26. },
  27. "file": {
  28. "level": "INFO",
  29. "class": "logging.FileHandler",
  30. "formatter": "console",
  31. "filename": os.path.expanduser("~/.piket_client.log"),
  32. },
  33. },
  34. "loggers": {
  35. "": {"handlers": ["console", "file"], "level": "INFO", "propagate": False},
  36. "piket_client": {"level": "INFO", "propagate": True},
  37. },
  38. }
  39. if HAVE_SENTRY and os.environ.get("SENTRY_DSN"):
  40. LOG_CONFIG["handlers"]["sentry"] = {
  41. "level": "ERROR",
  42. "class": "raven.handlers.logging.SentryHandler",
  43. "dsn": os.environ.get("SENTRY_DSN"),
  44. }
  45. LOG_CONFIG["loggers"][""]["handlers"].append("sentry")
  46. logging.config.dictConfig(LOG_CONFIG)