使用python日志记录模块将消息信息发送到一个文件,并将错误消息发送到另一个文件

问题描述:

我发现了一些类似的示例,可将消息记录到多个文件中.但是,对于我想要的东西,并没有什么.我正在使用日志记录模块,并且正在将所有信息消息记录到控制台和文件中. (我将在以后关闭控制台日志记录.)

I've found some similar example to log messages to multiple files. But nothing quite for what I want. I am using the logging module and I am logging all info messages to the console and a file. (I will turn off the console logging at a later date).

但是,我希望扩展它以将所有信息消息记录到file.log并将所有错误消息记录到file.err.如果可以将所有消息都记录到file.log(错误和信息)中,然后将所有错误消息记录到一个单独的文件中,则更好.

However I wish to expand this to log all info messages to file.log and all error messages to file.err. Even better if its possible to log all messages to file.log (error and info) and then all err messages to a separate file.

这可能吗?

显然,我七个小时都无法回答自己的问题,因此,我将只更新我的原始问题.

I can't answer my own question for 7 hours apparently, so I'll just update my original question.

我现在觉得很蠢.经过几个小时的尝试,然后再发布示例,我再次发现了这一点. 就像在阅读示例时键入我要尝试执行的任务一样,它会触发一些操作.

I feel stupid now. After several hours trying examples before posting I found this 'again'. Its like the task of typing out what I was trying to do triggered something when reading the examples.

http://docs.python.org/2/howto/logging- cookbook.html

这会将INFO级别及以上的所有内容输出到一个日志文件(log_file),然后还将logging.error和logging.warning消息打印到单独的文件(err_file)

This outputs everything at INFO level and above to one log file (log_file) and then also prints logging.error and logging.warning messages to a separate file (err_file)

# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.INFO,
                   format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                   filemode='w')

log_error = logging.FileHandler(err_file)
log_error.setLevel(logging.WARNING)

log_info = logging.FileHandler(log_file)
log_info.setLevel(logging.INFO)

# set a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')    

# tell the handler to use this format
log_error.setFormatter(formatter)    
log_info.setFormatter(formatter)        

# add the handler to the root logger
logging.getLogger('').addHandler(log_info)
logging.getLogger('').addHandler(log_error)

test_log.py:

import logging

def get_logger(    
        LOG_FORMAT     = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        LOG_NAME       = '',
        LOG_FILE_INFO  = 'file.log',
        LOG_FILE_ERROR = 'file.err'):

    log           = logging.getLogger(LOG_NAME)
    log_formatter = logging.Formatter(LOG_FORMAT)

    # comment this to suppress console output
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(log_formatter)
    log.addHandler(stream_handler)

    file_handler_info = logging.FileHandler(LOG_FILE_INFO, mode='w')
    file_handler_info.setFormatter(log_formatter)
    file_handler_info.setLevel(logging.INFO)
    log.addHandler(file_handler_info)

    file_handler_error = logging.FileHandler(LOG_FILE_ERROR, mode='w')
    file_handler_error.setFormatter(log_formatter)
    file_handler_error.setLevel(logging.ERROR)
    log.addHandler(file_handler_error)

    log.setLevel(logging.INFO)

    return log

def main():

    my_logger = get_logger()

    my_logger.info('This is an INFO message')
    my_logger.warning('This is a WARNING message')
    my_logger.error('This is an ERROR message')


if __name__ == '__main__':
    main()

输出

$ python test_log.py
2013-09-20 11:52:07,096 root         INFO     This is an INFO message
2013-09-20 11:52:07,096 root         WARNING  This is a WARNING message
2013-09-20 11:52:07,096 root         ERROR    This is an ERROR message

$ cat file.log
2013-09-20 11:52:07,096 root         INFO     This is an INFO message
2013-09-20 11:52:07,096 root         WARNING  This is a WARNING message
2013-09-20 11:52:07,096 root         ERROR    This is an ERROR message

$ cat file.err
2013-09-20 11:52:07,096 root         ERROR    This is an ERROR message