发布于 1年前

python 标准库 logging

今天是python的logging库,这是一个典型的日志库,用法简单实在。

首先是最简单的

import logging
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
# 输出
# this is warning
# this is error
# this is critical
# 之所以这里没有前两条是因为logging默认的日志级别是warning,只有大于等于这个日志级别的日志才会输出
# 这边需要重新键入代码,否则前面的日志库的对象是不会被清空的,会出现还是输出上面3条,要重新载入IPython
# 这时候我们才会有如下的结果
import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
# 输出
# this is debug
# this is info
# this is warning
# this is error
# this is critical

接下来是logging中basicConfig的一些参数使用

# filename    输出到文件的名字
# filemode    输出文件的格式,a,w,简单的两个
# format    基本语句的格式
# datefmt    日期的格式
# level    日志的级别
# stream    初始化的流
import logging
logging.basicConfig(filename="first.log",level=logging.DEBUG,filemode="a",format="%(asctime)s %(name)s %(levelname)s %(message)s",datefmt="%Y/%m/%d %H:%M:%S")
# format解释,以下为几个常用的format的关键字 %(key_words)s 都是基于这样形式包裹的
# asctime 时间
# created 毫秒值
# filename    日志在的地方
# funcName    日志所在函数的名字
# levelname    日志的级别
# levelno    日志的级别,用数字表示
# lineno    日志所在的行(运行文件的位置)
# module    模块名字
# msecs    毫秒值
# message    信息
# name    当前logger的名字
# pathname    日志运行文件的全路径
# process    进程id
# processName    进程名字(如果有的话)
# thread    线程id
# threadName    线程名字

# datefmt简单以下几个,需要更复杂的查python api去
# %Y 年, %m 月, %d 日 ,%H 小时, %M 分钟 %S 秒
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
# 结果出现在first.log上
# 2017/02/21 15:58:13 root DEBUG this is debug
# 2017/02/21 15:58:13 root INFO this is info
# 2017/02/21 15:58:13 root WARNING this is warning
# 2017/02/21 15:58:13 root ERROR this is error
# 2017/02/21 15:58:13 root CRITICAL this is critical

接下来就是稍微复杂的logger,handler,formatter

import logging

logger = logging.getLogger("sample")
# 注意所有的handler都要和logger的日志级别相同,否则会出现冗余现象哦,即打印一条出现很多条
logger.setLevel(logging.DEBUG)

sh = logging.StreamHandler() # 控制台输出流的handler
sh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(name) %(asctime)s %(levelname)s %(message)s")
sh.setFormatter(formatter)

fh = logging.FileHandler("sample.log",mode="a",encoding="utf-8",delay=0.5) # 文件流handler
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(levelname)s %(asctime)s %(message)s")
fh.setFormatter(formatter)

logger.addHandler(sh) # 一个logger可以有多个的handler
logger.addHandler(fh)

logger.debug("this is debug")
logger.info("this is info")
logger.error("this is error")
logger.warning("this is warning")
logger.critical("this is critical")
# 输出
# sample 2017-02-21 15:53:51,546 DEBUG this is debug
# sample 2017-02-21 15:53:51,550 INFO this is info
# sample 2017-02-21 15:53:51,552 ERROR this is error
# sample 2017-02-21 15:53:51,553 WARNING this is warning
# sample 2017-02-21 15:53:51,555 CRITICAL this is critical

最后的更复杂的日志,就自己查看官方文档了,例如日志的配置还是回滚日志等等,都很简单

©2020 edoou.com   京ICP备16001874号-3