-
-
Save zxc111/19e42af4a3bfb1f5dd97 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# coding=utf-8 | |
from tornado.options import options | |
from tornado.log import LogFormatter | |
import logging | |
from logging.handlers import RotatingFileHandler | |
import multiprocessing.queues | |
import threading | |
import sys | |
import traceback | |
class MultiProcessingLog(logging.Handler): | |
def __init__(self, filename, mode, maxBytes, rotate): | |
logging.Handler.__init__(self) | |
self._handler = RotatingFileHandler(filename, "a", maxBytes, rotate) | |
self.queue = multiprocessing.queues.SimpleQueue() | |
t = threading.Thread(target=self.receive) | |
t.daemon = True | |
t.start() | |
def setFormatter(self, fmt): | |
logging.Handler.setFormatter(self, fmt) | |
self._handler.setFormatter(fmt) | |
def receive(self): | |
while True: | |
try: | |
record = self.queue.get() | |
self._handler.emit(record) | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except EOFError: | |
break | |
except: | |
traceback.print_exc(file=sys.stderr) | |
def send(self, s): | |
self.queue.put(s) | |
def _format_record(self, record): | |
# ensure that exc_info and args | |
# have been stringified. Removes any chance of | |
# unpickleable things inside and possibly reduces | |
# message size sent over the pipe | |
if record.args: | |
record.msg = record.msg % record.args | |
record.args = None | |
if record.exc_info: | |
dummy = self.format(record) | |
record.exc_info = None | |
return record | |
def emit(self, record): | |
try: | |
s = self._format_record(record) | |
self.send(s) | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except: | |
self.handleError(record) | |
def close(self): | |
self._handler.close() | |
logging.Handler.close(self) | |
def enable_pretty_logging(options=None, logger=None): | |
"""Turns on formatted logging output as configured. | |
This is called automaticaly by `tornado.options.parse_command_line` | |
and `tornado.options.parse_config_file`. | |
""" | |
if options is None: | |
from tornado.options import options | |
if options.kanjian_logging == 'none': | |
return | |
if logger is None: | |
logger = logging.getLogger() | |
logger.setLevel(getattr(logging, options.kanjian_logging.upper())) | |
if options.log_file_prefix: | |
channel = MultiProcessingLog(# logging.handlers.RotatingFileHandler( | |
filename=options.log_file_prefix, | |
mode="a", | |
maxBytes=options.log_file_max_size, | |
rotate=options.log_file_num_backups,) | |
channel.setFormatter(LogFormatter(color=False)) | |
logger.addHandler(channel) | |
if (options.log_to_stderr or | |
(options.log_to_stderr is None and not logger.handlers)): | |
# Set up color if we are in a tty and curses is installed | |
channel = logging.StreamHandler() | |
channel.setFormatter(LogFormatter()) | |
logger.addHandler(channel) | |
def xx_define_logging_options(options=None): | |
if options is None: | |
# late import to prevent cycle | |
from tornado.options import options | |
options.define("kanjian_logging", default="info", | |
help=("Set the Python log level. If 'none', tornado won't touch the " | |
"logging configuration."), | |
metavar="debug|info|warning|error|none") | |
options.add_parse_callback(enable_pretty_logging) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment