在“日志记录”级别设置流和文件日志记录,并在“记录器”级别继承

时间:2018-07-08 19:08:49

标签: python logging

我想配置logging一次以将信息打印到stdout和具有两个不同级别的日志文件,而无需首先通过创建记录器对象来指定它们。我在网上找到的所有内容都有助于修改已创建的记录器对象,但是我想这样做,并稍后使用内置的这些参数创建多个记录器。

当前,我是通过创建一个记录器,然后在该记录器本身上设置选项来实现的。有没有一种方法可以设置一次,然后让每次调用logger = logging.getLogger(__name__)的日志都以预先配置的级别登录到两个目的地?

当前代码:

def get_logger(log_dir=constants.DEFAULT_LOGDIR,
               log_level=constants.DEFAULT_LOG_LEVEL):
    """ Set logger information """

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    # Set logging format
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - "
                                  "%(message)s","%Y.%m.%d-%H.%M.%S")

    # Create the logfile name and directories needed to hold it
    now = datetime.datetime.now()
    relative_logfile = "%04i/%02i/%02i/%04i.%02i.%02i-%02i.%02i.%02i"%(now.year,
                       now.month, now.day, now.year, now.month, now.day,
                       now.hour, now.minute, now.second)
    logfile_name = "{}/{}".format(log_dir, relative_logfile)
    if not os.path.exists(os.path.dirname(logfile_name)):
        os.makedirs(os.path.dirname(logfile_name))

    # Set information about the file handler
    logfile_handler = logging.FileHandler(logfile_name)
    logfile_handler.setLevel(logging.DEBUG)
    logfile_handler.setFormatter(formatter)

    # Create the user console handler with input log level
    console_handler = logging.StreamHandler()
    console_handler.setLevel(log_level)
    console_handler.setFormatter(formatter)

    # Attach both handlers
    logger.addHandler(logfile_handler)
    logger.addHandler(console_handler)

    return logger

我想将其更改为:

def set_logging(log_dir=constants.DEFAULT_LOGDIR,
               log_level=constants.DEFAULT_LOG_LEVEL):
    """ Set base logging information """
    # Set logging format
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - "
                                  "%(message)s","%Y.%m.%d-%H.%M.%S")

    # Create the logfile name and directories needed to hold it
    now = datetime.datetime.now()
    relative_logfile = "%04i/%02i/%02i/%04i.%02i.%02i-%02i.%02i.%02i"%(now.year,
                       now.month, now.day, now.year, now.month, now.day,
                       now.hour, now.minute, now.second)
    logfile_name = "{}/{}".format(log_dir, relative_logfile)
    if not os.path.exists(os.path.dirname(logfile_name)):
        os.makedirs(os.path.dirname(logfile_name))

    # Set information about the file handler
    logfile_handler = logging.FileHandler(logfile_name)
    logfile_handler.setLevel(logging.DEBUG)
    logfile_handler.setFormatter(formatter)

    # Create the user console handler with input log level
    console_handler = logging.StreamHandler()
    console_handler.setLevel(log_level)
    console_handler.setFormatter(formatter)

    ###########################################################
    # Somehow attach this information to 'logging' so that    #
    # it can be inherited much easier in the future and       #
    # the exact same file will be used (rather than creating  #
    # a new file each time).                                  #
    ###########################################################

0 个答案:

没有答案