Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import logging
  2. from flask import Flask
  3. from threading import Lock
  4.  
  5. # a lock used for logger initialization
  6. _logger_lock = Lock()
  7.  
  8.  
  9.  
  10. class MyCustomLogger(object):
  11. def __init__(self, app):
  12. self.instance = logging.getLogger(self.app.name)
  13. # Initialize logging stuff, you can use app.config here
  14.  
  15. def log_response(self, flask_response):
  16. # Can create/override logger stuff
  17. pass
  18. def __getattr__(self, name):
  19. """Forward any uncaught attributes to the instance object"""
  20. logger = self.instance
  21. if hasattr(logger, name):
  22. return getattr(logger, name)
  23. else:
  24. raise AttributeError('%s has no attribute "%s"' % (self, name))
  25.  
  26. class AltFlask(Flask):
  27.  
  28. """Subclass of Flask to customize behavior.
  29. This class is meant to be used in application factories. It modifies the
  30. behavior of the following:
  31. 1) Routing.
  32. 2) Exception logging.
  33. Subclassing Flask becomes necessary when an application grows, so it is
  34. generally a good idea to do it from the start.
  35. """
  36.  
  37. @property
  38. def logger(self):
  39. """Overrides the default logger property in Flask"""
  40. if self._logger and self._logger.name == self.logger_name:
  41. return self._logger
  42. with _logger_lock:
  43. if self._logger and self._logger.name == self.logger_name:
  44. return self._logger
  45. self._logger = rv = MyCustomLogger(self)
  46. return rv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement