python日志被压制

时间:2017-09-28 10:05:33

标签: python logging tornado

我的龙卷风应用程序正在使用多年前编写的一些遗留模块。这些模块配置为使用root logger注销事物。我面临的问题是,每当我导入这些模块时,由龙卷风打印的日志(即tornado.access,tornado.application等等)都会被抑制。

以下是我调用服务器的方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Basic run script"""

from zmq.eventloop import ioloop
ioloop.install()

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.autoreload
from tornado.options import options
import tornado.web

from grace_server.application import MyApplication
from settings import settings

def main():
    app = MyApplication(settings)
    app.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

以下是tornado.Application

的定义
import collections, zmq, os
import logging, re
import pickle, json
from datetime import datetime
from functools import partial

from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop import ioloop

from tornado import web
from tornado.log import LogFormatter, app_log, access_log, gen_log
from jupyter_client import MultiKernelManager

from legacy_module import api
from legacy_module.util.utils import get_env

from urls import url_patterns


ioloop = ioloop.IOLoop.current()

class MyApplication(web.Application):

    def __init__(self, settings):
        self.init_logging()
        self.connections = collections.defaultdict(list)
        self.kernels = {}
        self.listen_logs()
        web.Application.__init__(self, url_patterns, **settings)


    def init_logging(self):
        self.logger = logging.getLogger('MyApplication')
        self.logger.setLevel(logging.DEBUG)


    def broadcast_message(self, message):
        connections = self.connections.keys()
        for conn in connections:
            conn.write_message(message)

    def multicat_message(self, filter_, message):
        connections = self.connections.keys()
        connections = filter(connections)
        for conn in connections:
            conn.write_message(message)

    ...
    ...
    ...

这是logging

legacy_module的配置方式
import os, json
import logging, logging.config
from contextlib import contextmanager

from kombu import Connection
from terminaltables import AsciiTable

from legacy_module import resources
from legacy_module.resources.gredis import redis_tools
from legacy_module.core import versioning
from legacy_module.util.utils import get_logger_container, get_env

from legacy_module.resources.databases.mongo import MongoDatabaseCollection

DB_COLLECTION_OBJECT = MongoDatabaseCollection()

LOGGING_FILE = os.path.join(os.environ['legacy_module_HOME'], 'config', 'logging.config')
logging.config.fileConfig(LOGGING_FILE)
LOGGER = logging.getLogger()

...
...
...

这就是logging.config的样子。

[loggers]
keys = root

[handlers]
keys = consoleHandler

[formatters]
keys = simpleFormatter

[logger_root]
level = DEBUG
handlers = consoleHandler

[handler_consoleHandler]
class = StreamHandler
level = DEBUG
formatter = simpleFormatter
args = (sys.stdout,)

[formatter_simpleFormatter]
format = %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt = 

这是正常日志的样子

2017-09-28 02:40:03,409 MyApplication DEBUG    init_logging done
2017-09-28 02:40:13,018 MyApplication DEBUG    Authenticating

但当我从legacy_module注释掉MyApplication的导入时,我可以看到tornado.access日志

2017-09-28 02:40:03,409 MyApplication DEBUG    init_logging done
2017-09-28 02:40:13,017 tornado.access INFO     304 GET / (172.20.20.3) 1.79ms
2017-09-28 02:40:14,264 tornado.access INFO     304 GET /api/login (172.20.20.3) 0.75ms
2017-09-28 02:40:13,018 MyApplication DEBUG    Authenticating

所以我logging的{​​{1}}配置是legacy_module抑制日志的方式。 我该如何解决这个问题,我需要这些日志。

1 个答案:

答案 0 :(得分:0)

首先,在您的小型模块中,移除logging.config.fileConfig(LOGGING_FILE)来电并将LOGGER = logging.getLogger()替换为LOGGER = logging.getLogger(__name__)

然后你可能想要确保你至少正确配置了根记录器(不知道你在龙卷风中获得了什么来记录配置,所以检查文档)。

更一般的说明:库模块中的这种日志记录配置是记录反模式的完美示例 - logging包的重点是解耦记录器的使用(来自库代码中)从记录配置,应该使用库代码留给应用程序,并且应该可以根据应用程序实例进行配置。 FWIW注意到您自己的MyApplication.init_logging()也是反模式 - 您不应该对代码中的记录器级别进行硬编码,这应该使用每个实例配置来完成(参见django如何使用{ {1}}模块配置日志记录)。

<强>更新

我必须深入研究龙卷风的代码,以便为您提供详细的答案,但很明显,您的legacymodule中的settings调用会覆盖龙卷风自己的配置。

  

我在init_logging中完成的配置会被根记录器覆盖吗?

您目前唯一能够&#34;配置&#34; (并且你不应该在logging.config.fileConfig()中使用&#34; MyApplication&#34;记录器级别,这对使用哪个登录处理程序没有影响(=&gt;发送日志的位置)等。

  

我该如何预防?

这是我答案的第一部分......

相关问题