""" Logging for deliverance. This does not use the standard :mod:`logging` module because that module is not easily applied and inspected locally. We want the log messages to be strictly per-request. """ import logging from lxml.etree import tostring, _Element from tempita import HTMLTemplate, html_quote, html from deliverance.security import display_logging, edit_local_files NOTIFY = (logging.INFO + logging.WARN) / 2 logging.addLevelName(NOTIFY, 'NOTIFY') class SavingLogger(object): """ Logger that saves all its messages locally. """ def __init__(self, request, middleware): self.messages = [] self.middleware = middleware self.request = request # This is writable: self.theme_url = None # Also writable (list of (url, name)) self.edit_urls = [] def message(self, level, el, msg, *args, **kw): """Add one message at the given log level""" if args: msg = msg % args elif kw: msg = msg % kw self.messages.append((level, el, msg)) return msg def debug(self, el, msg, *args, **kw): """Log at the DEBUG level""" return self.message(logging.DEBUG, el, msg, *args, **kw) def info(self, el, msg, *args, **kw): """Log at the INFO level""" return self.message(logging.INFO, el, msg, *args, **kw) def notify(self, el, msg, *args, **kw): """Log at the NOTIFY level""" return self.message(NOTIFY, el, msg, *args, **kw) def warn(self, el, msg, *args, **kw): """Log at the WARN level""" return self.message(logging.WARN, el, msg, *args, **kw) warning = warn def error(self, el, msg, *args, **kw): """Log at the ERROR level""" return self.message(logging.ERROR, el, msg, *args, **kw) def fatal(self, el, msg, *args, **kw): """Log at the FATAL level""" return self.message(logging.FATAL, el, msg, *args, **kw) def finish_request(self, req, resp): """Called by the middleware at the end of the request. This gives the log an opportunity to add information to the page. """ if 'deliv_log' in req.GET and display_logging(req): resp.body += self.format_html_log() resp.cache_expires() return resp log_template = HTMLTemplate('''\
| Level | Message | Context |
|---|---|---|