网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

使用Logger保存到日志文件中

时间:2024-10-14 02:27:03

1、导入模块。import logging

2、获取Logger对象。loggers = logging.getLogger()

3、生成日志文件Handler。fh = logging.FileHandler('test.log')

4、设置记录文件的格式,并取其对象。formats = logging.Formatter('%(锾攒揉敫asctime)s - %(name)s - %(levelname)s - %(message)s')

5、使用文件格式对象设置文件Handler对象。fh.setFormatter(formats)

6、Logger对象加入日志文件Handler对象。loggers.addHandler(fh)

7、设置日志记录等级。loggers.setLevel(logging.DEBUG)

8、记袄嬖艽蛞录日志。logging.debug("hello world")logging.info("info hel造婷用痃lo")logging.warning("warning")logging.error("error word")logging.critical("bad m")

9、全部代码。

使用Logger保存到日志文件中使用Logger保存到日志文件中
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com