Python:logging日志模块

一、将日志打印到屏幕

1.2 logging概述

        logging是python的日志模块。 

        logging日志等级:

                CRITICAL  50
                ERROR     40
                WARNING   30
                INFO      20
                DEBUG     10
                NOTSET    0

        默认等级为:WARNING   

1.2 代码

#!/usr/bin/env python

import logging

logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")

1.3 运行结果


二、通过logging.basicConfig设置日志等级及输出格式等

2.1 logging.basicConfig概述

        format、datefmt各字段,见logging官网文档:https://docs.python.org/3/library/logging.html?highlight=logging#module-logging

2.2 代码

#!/usr/bin/env python

import logging

#logging.basicConfig
logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename="/opt/python-test/my.log",
                filemode="w")

logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")

2.3 运行结果


三、将日志同时输出到日志和屏幕

3.1. logging.StreamHandler概述

        继承自logging:

                StreamHandler         将日志输出到流,像sys.stdout,sys.stderr等

                FileHandler           将日志输出到磁盘文件

                NullHandler           将日志不输出

        继承自logging.handlers:

                WatchedFileHandler    监控日志文件,如果关闭了,重新打开

        继承自logging.handlers:

                SocketHandler 将日志输出到网络,通过TCP

                DatagramHandler 将日志输出到网络,通过UDP

                SysLogHandler将日志发送到远程或本地的Unix syslog

                NTEventLogHandler 将日志发送到Windows event log

                SMTPHandler 将日志发送到邮箱,通过SMTP

                MemoryHandler 

                HTTPHandler 将日志发送到Web服务器,通过GET或POST

                QueueHandler 

                QueueListener 

3.2 代码

#!/usr/bin/env python

import logging

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename="/opt/python-test/my.log",
                filemode="w")

#logging.StreamHandler()
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")

3.3 运行结果


四、日志回滚

4.1 RotatingFileHandler概述

        继承自logging.handlers:

                BaseRotatingHandler      磁盘文件切割的基类

                RotatingFileHandler      磁盘文件切割

                TimedRotatingFileHandler 磁盘文件切割,按时间间隔切割

4.2 代码

#!/usr/bin/env python

import logging
from logging.handlers import RotatingFileHandler #RotatingFileHandler

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename="/opt/python-test/my.log",
                filemode="w")

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

#RotatingFileHandler
rotate = RotatingFileHandler("myrotate.log", maxBytes=30, backupCount=2)
rotate.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
rotate.setFormatter(formatter)
logging.getLogger('').addHandler(rotate)

logging.debug("This is debug message")
logging.info("This is info message")
logging.warning("This is warning message")

4.3 运行结果



参考资料:

        python的日志logging模块学习:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

        python的logging官网文档:https://docs.python.org/3/library/logging.html?highlight=logging#module-logging

        python的logging.handlers官网文档:https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler

猜你喜欢

转载自blog.csdn.net/kanguolaikanguolaik/article/details/50637362