messenger.py 857 B

1234567891011121314151617181920212223242526272829303132
  1. import logging
  2. from flask import current_app
  3. from lakesuperior.messaging import formatters, handlers
  4. messenger = logging.getLogger('_messaging')
  5. class Messenger:
  6. '''
  7. Very simple message sender.
  8. '''
  9. _msg_routes = []
  10. def __init__(self, config):
  11. for route in config['routes']:
  12. handler_cls = getattr(handlers, route['handler'])
  13. messenger.addHandler(handler_cls(route['endpoint']))
  14. messenger.setLevel(logging.INFO)
  15. #messenger.formatter = logging.Formatter('%(message)s')
  16. formatter = getattr(formatters, route['formatter'])
  17. self._msg_routes.append((messenger, formatter))
  18. def send(self, *args, **kwargs):
  19. '''
  20. Send one or more external messages.
  21. '''
  22. for m, f in self._msg_routes:
  23. m.info(f(*args, **kwargs))