Is there a neat way to integrate lithoxyl into flask.logger?
<p>I like the look of <a href="http://lithoxyl.readthedocs.io/en/latest/index.html" rel="nofollow">lithoxyl</a> and would like to progressively replace my current usage of <a href="http://flask.pocoo.org/docs/0.11/api/#flask.Flask.logger" rel="nofollow">flask.logger</a> with it.</p>

<p>Is there a good way to get the two logging frameworks to coexist?</p>

<p>So far I have the following:</p>

<pre><code>from flask import current_app
from werkzeug.local import LocalProxy

logger = LocalProxy(lambda: current_app.logger)

class LogAdaptor(object):
    """file-like object that will write messages to the logger"""
    def write(self, msg):
        if msg.strip():
            logger.info(msg)

from lithoxyl import StreamEmitter, SensibleFormatter

emtr = StreamEmitter(LogAdaptor())

fmtr = SensibleFormatter('{level_name_upper} {module_name} {end_message}')

# ... the rest is basically the same as http://lithoxyl.readthedocs.io/en/latest/overview.html#logging-sensibly
</code></pre>

<p>This more or less works but the log level is lost in the output, e.g.:</p>

<pre><code>DEBUG backend: log message from app.logger.debug
INFO logger: CRITICAL "backend" "critical action failed"
INFO logger: DEBUG "backend" "action succeeded"
DEBUG backend: log message from app.logger.debug
</code></pre>

<p>I'm guessing what's needed is a more complex emitter, or a way to access the .write of the underlying stream in the Flask.logger handler (bypassing the formatting etc.)</p>

<p>Or is this all just barking up the wrong tree and I should just live with split log files until it's all refactored?</p>

<p>I've managed to make some improvements as I found <a href="https://github.com/mahmoud/lithoxyl/blob/master/lithoxyl/_syslog_emitter.py" rel="nofollow">https://github.com/mahmoud/lithoxyl/blob/master/lithoxyl/_syslog_emitter.py</a> and created a similar class:</p>

<pre><code>import logging
from lithoxyl.common import DEBUG, INFO, CRITICAL, get_level

class LoggerEmitter(object):
    priority_map = {DEBUG: {'success': logging.DEBUG,
                            'failure': logging.INFO,
                            'warn': logging.INFO,
                            'exception': logging.WARNING},
                    INFO: {'success': logging.INFO,
                           'failure': logging.WARNING,
                           'warn': logging.WARNING,
                           'exception': logging.ERROR},
                    CRITICAL: {'success': logging.WARNING,
                               'failure': logging.ERROR,
                               'warn': logging.ERROR,
                               'exception': logging.CRITICAL}}

    def __init__(self, logger):
        self.logger = logger

    def on_begin(self, begin_event, entry):
        level = self._get_level('begin', begin_event)
        self.logger.log(level, entry)

    def on_warn(self, warn_event, entry):
        level = self._get_level('warn', warn_event)
        self.logger.log(level, entry)

    def on_end(self, end_event, entry):
        level = self._get_level('end', end_event)
        self.logger.log(level, entry)

    def _get_level(self, event_name, action):
        level = get_level(action.level)

        if event_name == 'warn':
            status = 'warn'
        elif event_name == 'begin':
            status = 'begin'
        else:
            status = action.status

        return self.priority_map[level][status]

emtr = LoggerEmitter(logger)
</code></pre>

<p>The output is now something like:</p>

<pre><code>DEBUG backend: log message from app.logger.debug
ERROR logger: "backend" "critical action failed"
DEBUG logger: "backend" "action succeeded"
DEBUG backend: log message from app.logger.debug
</code></pre>

<p>Which is better, but it would be nice if the module name was passed through properly too.</p>

python logging flask lithoxyl