日志
发布日期:2022-07-08 02:55:50 浏览次数:31 分类:技术文章

本文共 1205 字,大约阅读时间需要 4 分钟。

日志

1、为什么有日志?
给开发人员错误,用于排查错误。
2、只能往一个文件中写:
import traceback #获取当前错误的堆栈信息

import loggingimport traceback     # 把错误的堆栈写到日志logger = logging.basicConfig(filename='xxx.txt',                             format='%(asctime)s - %(name)s - %(module)s: %(message)s',                             datefmt='%Y-%m-%d %H-%M-%S',                             level=30)logging.debug('x1')logging.info('x2')logging.warning('x3')logging.error('x4')logging.critical('x5')def func():    try:        a = a + 1    except Exception as e:        msg = traceback.format_exc()        logging.error(msg)func()

3、往多个文件写的方法:

import logging# 定义文件file_1_1 = logging.FileHandler('l1_1.log', 'a', encoding='utf-8')fmt1 = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")file_1_1.setFormatter(fmt1)file_1_2 = logging.FileHandler('l1_2.log', 'a', encoding='utf-8')fmt2 = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")file_1_2.setFormatter(fmt2)# 定义日志logger1 = logging.Logger('s1', level=logging.ERROR)logger1.addHandler(file_1_1)logger2 = logging.Logger('s2', level=logging.ERROR)logger2.addHandler(file_1_2)# 写日志logger1.critical('1111')logger2.critical('2222')

转载地址:https://blog.csdn.net/weixin_43173892/article/details/85665999 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:日志
下一篇:日志

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月19日 00时37分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章