Python3.6日志Logging模块简单用法示例
作者:Tyran_U 时间:2021-03-18 06:47:38
本文实例讲述了Python3.6日志Logging模块简单用法。分享给大家供大家参考,具体如下:
Logging是一个很方便的模块,用来打印日志
我直接列出一个最灵活的方法
# -*- coding:utf-8 -*-
#!python3
import logging
logger = logging.getLogger() # logging对象
fh = logging.FileHandler("test.log") # 文件对象
sh = logging.StreamHandler() # 输出流对象
fm = logging.Formatter('%(asctime)s-%(filename)s[line%(lineno)d]-%(levelname)s-%(message)s') # 格式化对象
fh.setFormatter(fm) # 设置格式
sh.setFormatter(fm) # 设置格式
logger.addHandler(fh) # logger添加文件输出流
logger.addHandler(sh) # logger添加标准输出流(std out)
logger.setLevel(logging.DEBUG) # 设置从那个等级开始提示
logger.debug("debug Test")
logger.info("info Test")
logger.warning("warning Test")
logger.error("error Test")
logger.critical("critical Test")
运行结果:
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/tyrantu1989/article/details/78412304
标签:Python3,日志,Logging模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IE6局部调用PNG32合并图片
2009-03-11 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-12s.jpg)
Python QTimer实现多线程及QSS应用过程解析
2023-10-26 12:34:46
python实现简单的tcp 文件下载
2022-08-03 02:38:09
Python测试网络连通性示例【基于ping】
2023-09-16 13:48:38
![](https://img.aspxhome.com/file/2023/1/131111_0s.png)
python中对开区间和闭区间的理解
2022-02-09 17:47:29
python 每天如何定时启动爬虫任务(实现方法分享)
2022-11-28 21:14:35
Python Django 母版和继承解析
2023-02-21 09:44:59
![](https://img.aspxhome.com/file/2023/6/76016_0s.png)
关于设计的一些个人看法
2008-06-12 12:44:00
从算法入手讲解SQL Server的典型示例
2008-12-18 14:51:00
python 怎样进行内存管理
2021-04-27 16:46:18
匿名函数的写法
2010-01-06 13:32:00
Python numpy和matlab的几点差异介绍
2023-12-14 23:26:39
JavaScript设计模式之适配器模式介绍
2023-08-12 06:36:09
python中bisect模块用法实例
2023-03-13 16:23:42
pytorch教程网络和损失函数的可视化代码示例
2023-11-26 16:13:51
![](https://img.aspxhome.com/file/2023/0/91970_0s.png)
细化解析:MySQL 服务器究竟有多稳定?
2009-01-14 11:55:00
Python使用Django实现博客系统完整版
2021-02-10 14:43:48
![](https://img.aspxhome.com/file/2023/3/95393_0s.png)
如何用Pytorch搭建一个房价预测模型
2022-12-16 17:09:21
![](https://img.aspxhome.com/file/2023/5/127725_0s.png)
详解python3安装pillow后报错没有pillow模块以及没有PIL模块问题解决
2021-08-28 15:42:10
![](https://img.aspxhome.com/file/2023/3/85043_0s.png)
我的css样式写法总结
2009-01-18 13:04:00