python logging—模块

python logging模块

python logging提供了标准的日志接口,python logging日志分为5个等级:

debug(), info(), warning(), error() and critical()

简单用法

import logging
logging.warning("warning.........")
logging.critical("server is down")

print:

WARNING:root:warning.........
CRITICAL:root:server is down

5个日志级别所代表的意思:

Level 说明
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.

把日志写到文件里

import logging

logging.basicConfig(filename="log_test.log", level=logging.INFO) #此处定义了日志级别,INFO以及高于INFO的日志会被记录
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")

结果:

我们把日志级别调整一下:

import logging

logging.basicConfig(filename="log_test.log", level=logging.DEBUG) #level 改为 debug
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")

结果:

debug 级别的日志已经记录了。另外,日志的写入方式是追加,不会覆盖之前的日志。


自定义日志格式

日志格式参数:

%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s 用户输出的消息

上段代码看一下:

import logging

logging.basicConfig(filename="log_test.log",
                    level=logging.DEBUG,
                    format="%(asctime)s - %(levelname)s - %(module)s - %(lineno)d %(message)s",
                    datefmt="%Y-%m-%d %I:%M:%S:%p"
                    )

def fun1():
    logging.error("error......")

fun1()
logging.debug("debug..............")
logging.info("info..............")
logging.warning("warning...........")



结果:


高级用法

 1.生成logger对象
logger = logging.getLogger("web")
logger.setLevel(logging.INFO) #设置日志级别。默认日志级别为 warning
# 2.生成handler对象
console_handler = logging.StreamHandler()  # 用于打印的handler
console_handler.setLevel(logging.WARNING) #也可以专门针对 handler设置日志级别
file_handler = logging.FileHandler("web.log")  # 用于输出到文件的 handler
file_handler.setLevel(logging.ERROR)
# 2.1 把handler 对象 绑定到 logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 3.生成formatter 对象
console_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s")
# 3.1 把formatter 对象 绑定到 handler
console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)

logger.info("info----")
logger.warning("warning-------------")
logger.error("error-------")


全局设置的日志级别 和 handler等设置的日志级别,是逐级过滤的。

比如:

  • 全局设置的级别是 info ,handler 设置的级别是 debug , 到最后,debug级别的日志会被过滤掉
  • 全局设置的界别是 info,handler设置的级别是 error ,到最后,error已经高于error 级别的日志会被输出

过滤日志:

# *_*coding:utf-8 *_*
import logging

class IngonreBackupLogFilter(logging.Filter):
    """查找带 db backup 的日志"""
    def filter(self, record):#固定写法
        return "db backup" in record.getMessage()
# 1.生成logger对象
logger = logging.getLogger("web")
logger.setLevel(logging.DEBUG) #设置日志级别。默认日志级别为 warning
# 1.1 把filter对象添加到logger中
logger.addFilter(IngonreBackupLogFilter())
# 2.生成handler对象
console_handler = logging.StreamHandler()  # 用于打印的handler
file_handler = logging.FileHandler("web.log")  # 用于输出到文件的 handler
# 2.1 把handler 对象 绑定到 logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 3.生成formatter 对象
console_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s")
# 3.1 把formatter 对象 绑定到 handler
console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)

logger.info("info----")
logger.warning("warning-------------")
logger.error("error-------")
logger.error("error db backup ----")

# 打印:
# 2018-07-08 20:10:40,023 - web - ERROR - error db backup ----

文件自动截断:

import logging
from logging import handlers
logger = logging.getLogger(__name__)
log_file = "timelog.log"
# file_handler = handlers.RotatingFileHandler(filename=log_file, maxBytes=10, backupCount=3) 
file_handler = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)

formatter = logging.Formatter('%(asctime)s - %(module)s - %(lineno)d - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

logger.warning("test1")
logger.warning("test2")
logger.warning("test3")
logger.warning("test4")

结果:会按时间生成不同的日志文件:

file_handler = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)

You can use the when to specify the type of interval. The list of possible values is below. Note that they are not case sensitive.

(你可以使用when来指定interval的类型。可能的值列表如下。注意它们不是区分大小写的。 )

(这个英语还是有很有必要花时间去搞一下的)

Value Type of interval
'S' Seconds
'M' Minutes
'H' Hours
'D' Days
'W' Week day (0=Monday)
'midnight' Roll over at midnight 午夜roll over
posted @ 2018-07-12 18:14  郭东东郭  阅读(167)  评论(0编辑  收藏  举报