2017-09-28 29 views
2

我的龍捲風應用程序正在使用一些早在幾年前編寫的遺留模塊。這些模塊被配置爲使用根記錄器註銷事情。我面臨的問題是,無論何時我導入這些模塊,龍捲風打印的日誌(即tornado.access,tornado.application等)都會受到抑制。python日誌被壓制

這是我如何調用我的服務器

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

這裏是如何logginglegacy_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() 

... 
... 
... 

這是配置了的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.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 

這樣的logging配置我的legacy_module是一些如何通過tornado抑制日誌。 我該如何解決這個問題,我需要這些日誌。

回答

0

首先,在yourlegacymodule中,刪除logging.config.fileConfig(LOGGING_FILE)呼叫並用LOGGER = logging.getLogger(__name__)替換LOGGER = logging.getLogger()

然後,您可能要確保至少有根記錄器已正確配置(不知道您在龍捲風中獲取的日誌配置信息,請查看文檔)。

作爲更一般的說明:庫模塊中的這種日誌記錄配置是記錄反模式的完美示例 - logging軟件包的全部要點是從日誌記錄配置中分離記錄器的使用(從庫代碼中)到使用庫代碼的應用程序,並且應該可配置爲每個應用程序實例。 FWIW請注意,您自己的MyApplication.init_logging()也是反模式 - 您不應該在代碼中對日誌記錄器的級別進行硬編碼,這應該使用每個實例的配置來完成(請參閱django如何使用settings模塊來配置日誌記錄)。

更新:

我不得不深入到龍捲風的代碼給你一個確切的詳細的答案,但顯然logging.config.fileConfig()呼叫yourlegacymodule覆蓋龍捲風自己的配置。

是我在init_logging中完成的配置被root logger覆蓋嗎?

您目前「配置」(和你不應該)在init_logging的唯一的事是「MyApplication的」記錄儀的水平,這有沒有影響上洛處理程序使用(=>在您的日誌發送)等

我該如何預防它們?

這是我回答的第一個部分......

+0

感謝您的回答@bruno,我會保持這種反模式在我的腦海現在,但你能到我爲什麼我可以解釋看不到日誌,我的配置是否在'init_logging'中被root記錄器覆蓋,如果有的話我怎麼能夠阻止它們呢?或者日誌正在流向其他地方? –

+0

比較我編輯的答案。 –