Python logger模块应用示例

同时输出到文件和终端,并设置不同的输出级别

#! /usr/bin/env python
# coding: utf-8
import os
import logging

class MyLogger(logging.Logger):
    def __init__(self, filename='log/test.log'):
        # super(MyLogger, self).__init__(filename)
        logging.Logger.__init__(self, filename)

        # 设置日志格式
        fmtHandler = logging.Formatter('%(asctime)s [%(filename)s:%(lineno)s][%(levelname)s] %(message)s')

        # 终端log输出流设置
        try:
            consoleHd = logging.StreamHandler()
            consoleHd.setLevel(logging.ERROR)
            consoleHd.setFormatter(fmtHandler)
            self.addHandler(consoleHd)
        except Exception as reason:
            self.error("%s" % reason)

        # 设置log文件
        try:
            os.makedirs(os.path.dirname(filename))
        except Exception as reason:
            pass
        try:
            fileHd = logging.FileHandler(filename)
            fileHd.setLevel(logging.INFO)
            fileHd.setFormatter(fmtHandler)
            self.addHandler(fileHd)
        except Exception as reason:
            self.error("%s" % reason)            

        return
        # 设置回滚日志,每个日志最大10M,最多备份5个日志
        try:
            rtfHandler = logging.BaseRotatingHandler(
                            filename, maxBytes=10*1024*1024, backupCount=5)
        except Exception as reason:
            self.error("%s" % reason)
        else:
            self.addHandler(rtfHandler)

def main():
    logfile1 = 'log/test1.log'
    logger1 = MyLogger(logfile1)
    logger1.debug("debug msg")
    logger1.info("info msg")
    logger1.warning("warning msg")
    logger1.error("error msg")
    logger1.critical("critical msg")

    logfile2 = 'log/test2.log'
    logger1 = MyLogger(logfile2)
    logger1.debug("debug msg")
    logger1.info("info msg")
    logger1.warning("warning msg")
    logger1.error("error msg")
    logger1.critical("critical msg")

if __name__ == '__main__':
    main()



本例封装了类,在类中进行了统一的配置

终端输出

2017-02-11 10:32:01,532 [mylogger.py:52][ERROR] error msg
2017-02-11 10:32:01,532 [mylogger.py:53][CRITICAL] critical msg
2017-02-11 10:32:01,533 [mylogger.py:60][ERROR] error msg
2017-02-11 10:32:01,533 [mylogger.py:61][CRITICAL] critical msg

log1文件中记录

2017-02-11 10:32:01,532 [mylogger.py:50][INFO] info msg
2017-02-11 10:32:01,532 [mylogger.py:51][WARNING] warning msg
2017-02-11 10:32:01,532 [mylogger.py:52][ERROR] error msg
2017-02-11 10:32:01,532 [mylogger.py:53][CRITICAL] critical msg 







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