Python记录失败

编程入门 行业动态 更新时间:2024-10-20 13:44:18
本文介绍了Python记录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

只是开始查看日志记录模块,并创建了一个虚拟程序来了解记录器,处理程序和格式化程序.这是代码

Just started to look up on the logging module and created a dummy program to understand the logger, handler and formatter. Here is the code

# logging_example.py import logging from datetime import datetime import os extension = datetime.now().strftime("%d-%b-%Y_%H_%M_%S_%p") logfile = os.path.join("logs", f"demo_logging_{extension}.txt") logger = logging.getLogger(__name__) ch = logging.StreamHandler() fh = logging.FileHandler(logfile) ch.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") ch.setFormatter(formatter) fh.setFormatter(formatter) logger.addHandler(ch) logger.addHandler(fh) logger.info("Hello World")

当我执行程序时,日志目录中包含文件,但内容为空,并且屏幕上没有任何内容.我很确定我缺少一些基本的知识,但是:(..

When i execute the program the logs directory has the files but content is empty and nothing gets printed on the screen to. I am pretty sure I am missing something basic but am not able to catch it though :( .

我将不胜感激.

谢谢

推荐答案

您已将日志级别添加到处理程序中,但未添加到记录器中.这意味着,如果记录程序将其传递给处理程序,则处理程序将记录该消息.但是由于记录器阈值较高,因此下降了.

You have added log-level to the handlers but not to the logger. Which means, handler would have logged the message had the logger passed it. But since the logger threshold is higher it got dropped.

查看此链接

还将日志级别添加到记录器.添加处理程序后:

Add log-level to the logger also. After adding the handlers:

logger.setLevel(logging.DEBUG)

更多推荐

Python记录失败

本文发布于:2023-10-24 14:05:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!