导入模块的单个文件处理程序

时间:2016-10-06 14:29:26

标签: python logging

我在根级模块中为记录器定义了一个fileHandler,我希望子模块将日志消息写入同一位置。我能够这样做的唯一方法是复制子模块中的fileHandler并将其添加到模块记录器中。有没有办法定义文件处理程序一次?

text_analyzer.py(根级模块):

for store: Dictionary<String, String> in storeDataArray {
    // Do things with store["store_id"] here
}

text_scrapper.py(子模块):

import logging
import logging.handlers
import text_scrapper

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name) - 25s - %(levelname)s - %(message)s')

stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)

logFilePath = PATH_TO_LOG_FILE
file_handler = logging.handlers.TimedRotatingFileHandler(filename=logFilePath, when='midnight', backupCount=30)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)

logger.addHandler(file_handler)
logger.addHandler(stream_handler)


class TextAnalyzer(object):
    def __init__(self):
        self.ta_logger = logging.getLogger(__name__+".TextAnalyzer")
        self.ta_logger.info("creating TextAnalyzer object")
        self.scrapper = text_scrapper.Scrapper()
        self.data = None
        self.result = []

    def start(self):
        self.get_data_from_scrapper()
        self.calculate_result()

    def get_data_from_scrapper(self):
        self.ta_logger.info("getting data")
        self.data = self.scrapper.scrape_data()

    def calculate_result(self):
        self.ta_logger.info("calculating result")
        #calculate result


if __name__ == '__main__':
    logger.info("starting program")
    a = TextAnalyzer()
    a.start()

这是text_analyzer.log,其中包含text_scraper.py中的注释代码:

import logging
import logging.handlers

module_logger = logging.getLogger(__name__)
module_logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name) - 25s - %(levelname)s - %(message)s')

module_stream_handler = logging.StreamHandler()
module_stream_handler.setLevel(logging.WARNING)
module_stream_handler.setFormatter(formatter)

#logFilePath = PATH_TO_LOG_FILE
#file_handler = logging.handlers.TimedRotatingFileHandler(filename=logFilePath, when='midnight', backupCount=30)
#file_handler.setFormatter(formatter)
#file_handler.setLevel(logging.DEBUG)
#module_logger.addHandler(file_handler)

module_logger.addHandler(module_stream_handler)

class Scrapper(object):
    def __init__(self):
        self.scrapper_logger = logging.getLogger(__name__ + ".Scrapper")
        self.scrapper_logger.info("created Scrapper object")
        self.text_data = []
        pass

    def scrape_data(self):
        text = ''
        self.scrapper_logger.info("Scrapping Data")
        # do some scrapping and return some text
        self.text_data = process_text(text)

def process_text(text):
    module_logger.info("processing text")
    if text == '':
        module_logger.warning("empty text")
    return text

这是text_analyzer.log,其中包含text_scrapper.py中未注释的代码:

2016-10-06 10:22:03,863 - __main__                  - INFO - starting program
2016-10-06 10:22:03,864 - __main__.TextAnalyzer     - INFO - creating TextAnalyzer object
2016-10-06 10:22:03,865 - __main__.TextAnalyzer     - INFO - getting data
2016-10-06 10:22:03,866 - __main__.TextAnalyzer     - INFO - calculating result

0 个答案:

没有答案