logging 是 Python 自带的,不需要导包
logging 相当于print 的功能 ,实际运用中,print不合适,所以用 logging
命名 logging_use.py 用到的使用直接复制过去就可以
import logging.handlers
import logging
import time
def init_log_config(filename, when='midnight', interval=1, backup_count=7):
"""
功能:初始化日志配置函数
:param filename: 日志文件名
:param when: 设定日志切分的间隔时间单位
:param interval: 间隔时间单位的个数,指等待多少个 when 后继续进行日志记录
:param backup_count: 保留日志文件的个数
:return:
"""
# 1. 创建日志器对象
logger = logging.getLogger()
# 2. 设置日志打印级别
logger.setLevel(logging.DEBUG) # 设置级别,只能打印 向下的级别,向上的级别打印不出来
# logging.DEBUG 调试级别
# logging.INFO 信息级别
# logging.WARNING 警告级别
# logging.ERROR 错误级别
# logging.CRITICAL 严重错误级别
# 3. 创建处理器对象
# 控制台对象
st = logging.StreamHandler()
# 日志文件对象
fh = logging.handlers.TimedRotatingFileHandler(filename,
when=when,
interval=interval,
backupCount=backup_count,
encoding='utf-8')
# 4. 日志信息格式
fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
formatter = logging.Formatter(fmt)
# 5. 给处理器设置日志信息格式
st.setFormatter(formatter)
fh.setFormatter(formatter)
# 6. 给日志器添加处理器
logger.addHandler(st)
logger.addHandler(fh)
if __name__ == '__main__':
# 1.初始化日志 必须要做 没有信息做不了
# filename, when='midnight', interval=1, backup_count=7
# 日志名字 什么时间:半夜 每一天生成一次文件 备份:近7份文件
init_log_config('abc.log', when='midnight', interval=1, backup_count=7)
# 2. 打印输出日志信息 ,必须要做 指定级别
# logging.debud('xxxx - debug 日志信息')
# logging.debug('一个调试级别的日志')
# a = 100 # “a”相当于替换 日志信息格式中的“message”中的值 %(message)s"
# logging.error(f'日志信息的测试 a={a}') # 设置级别,只能打印 向下的级别,向上的级别打印不出来
# logging.debug('设置‘error’错误级别,打印一个‘debug’调试级别') # 打印不出来
# 调回去 “ # 2. 设置日志打印级别 logger.setLevel(logging.DEBUG) ”
logging.debug('一个调试级别的日志')
abc.log 文件打印结果
2022-07-10 10:01:51,438 DEBUG [logging_use.py(<module>:55)] - 一个调试级别的日志
2022-07-10 10:04:51,073 DEBUG [logging_use.py(<module>:57)] - 一个调试级别的日志
2022-07-10 10:06:37,542 INFO [logging_use.py(<module>:59)] - 日志信息的测试 a=100
2022-07-10 10:11:12,593 ERROR [logging_use.py(<module>:59)] - 日志信息的测试 a=100
2022-07-10 10:35:14,572 DEBUG [logging_use.py(<module>:62)] - 一个调试级别的日志
使用在,项目,举例
import unittest
import logging
from test_logging.logging_use import init_log_config
def add(x, y):
return x + y
class TestAddLog(unittest.TestCase):
# 使用之前,必须要进行“一次”初始化
@classmethod
def setUpClass(cls) -> None:
cls.l = init_log_config('add.log')
@classmethod
def tearDownClass(cls) -> None:
cls.l = init_log_config('add.log')
def test01_add(self):
res = add(10, 20)
logging.debug(f"res={res}")
def test02_add(self):
res = add(29, 25)
logging.debug(f"res={res}")
日志结果
2022-07-10 10:39:45,316 INFO [tes_log.py(test01_add:13)] - res=30
2022-07-10 10:40:39,142 DEBUG [tes_log.py(test01_add:13)] - res=30
2022-07-10 10:48:18,417 DEBUG [tes_log.py(test01_add:15)] - res=30
2022-07-10 10:50:08,749 DEBUG [tes_log.py(test01_add:16)] - res=30
2022-07-10 10:59:06,024 DEBUG [tes_log.py(test01_add:20)] - res=30
2022-07-10 11:02:40,237 DEBUG [tes_log.py(test01_add:20)] - res=30
2022-07-10 11:02:40,241 DEBUG [tes_log.py(test02_add:24)] - res=54
2022-07-10 11:07:54,759 DEBUG [tes_log.py(test01_add:20)] - res=30
2022-07-10 11:07:54,763 DEBUG [tes_log.py(test02_add:24)] - res=54