python logging 日志

日志模块定义

import logging


class MyLogging(object):
    """
    封装后的logging
    """

    def __init__(self, logger=None):
        # 创建一个logger
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)

        # 再创建一个handler,用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter(
            '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]: %(message)s')

        ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(ch)

        #  添加下面一句,在记录日志之后移除句柄
        # self.logger.removeHandler(ch)
        # self.logger.removeHandler(fh)
        # 关闭打开的文件
        ch.close()

    def get_log(self):
        return self.logger

日志模块引用

from utils.logger import *
import pandas as pd

logger = MyLogging().get_log()


def test():
    logger.info("test log.....")


if __name__ == '__main__':
    logger.info("main log........")
    test()

你可能感兴趣的:(Python,python)