python日志被抑制

2024-04-23 13:56:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我的tornado应用程序使用了许多年前编写的一些遗留模块。这些模块配置为使用根记录器注销。我所面临的问题是,每当我导入这些模块的日志打印的龙卷风(即。龙卷风通道, tornado.应用等…)被压制。你知道吗

下面是我如何调用我的服务器

#!/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

但是当我从MyApplication注释掉legacy_module的导入时,我可以看到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

所以我的legacy_modulelogging配置是通过tornado来抑制日志的。 我该怎么修,我需要这些日志。你知道吗


Tags: fromdebugimportselfwebmessagesettingsinit
1条回答
网友
1楼 · 发布于 2024-04-23 13:56:18

首先,在legacyModule中,删除logging.config.fileConfig(LOGGING_FILE)调用并用LOGGER = logging.getLogger(__name__)替换LOGGER = logging.getLogger()。你知道吗

然后,您可能需要确保至少正确配置了根记录器(不知道您在tornado中得到了什么日志配置,所以请查看文档)。你知道吗

更一般的说明是:库模块中的这个日志配置是记录反模式的完美例子logging包的全部要点是将日志记录器的使用(从库代码中)与日志配置分离开来,日志配置应该留给使用库代码的应用程序,并且每个应用程序都可以配置实例。FWIW注意,您自己的MyApplication.init_logging()也是一个反模式-您不应该在代码中硬编码记录器的级别,这应该使用每个实例配置来完成(参见django如何使用settings模块来配置日志)。你知道吗

更新:

我必须深入研究tornado的代码才能给您一个准确详细的答案,但显然legacyModule中的logging.config.fileConfig()调用会覆盖tornado自己的配置。你知道吗

are my configs done in init_logging get overridden by the root logger ?

当前在init_logging中唯一“配置”(不应该配置)的是“MyApplication”logger的级别,这对使用哪个loggin处理程序(=>;发送日志的位置)等没有影响

how can I prevent them ?

这是我回答的第一部分。。。你知道吗

相关问题 更多 >