Last active
May 20, 2017 10:52
-
-
Save malexer/06fd8f6530a02fddc5feb7c1f2628799 to your computer and use it in GitHub Desktop.
Redirect all logging (including errors) from Tornado web framework to stdout and use stderr for error logs only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Redirect all logging (including errors) from Tornado web framework to stdout | |
and use stderr for error logs only. | |
Can be usefull when you want to store separatelly all logs and error logs. | |
""" | |
import logging | |
import sys | |
import tornado.log | |
from tornado.options import options | |
def setup_logging_to_stream(stream, log_level): | |
logger = logging.getLogger() | |
channel = logging.StreamHandler(stream) | |
channel.setLevel(log_level) | |
channel.setFormatter(tornado.log.LogFormatter()) | |
logger.addHandler(channel) | |
def setup_logging(log_level=None): | |
if log_level is None: | |
log_level = getattr(logging, options.logging.upper()) | |
logger = logging.getLogger() | |
logger.setLevel(log_level) | |
setup_logging_to_stream(stream=sys.stdout, log_level=log_level) | |
setup_logging_to_stream(stream=sys.stderr, log_level=logging.ERROR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment