python实现log日志的示例代码
作者:shengnan_only 发布时间:2023-11-29 13:25:49
标签:python,log日志
源代码:
# coding=utf-8
import logging
import os
import time
LEVELS={'debug':logging.DEBUG,\
'info':logging.INFO,\
'warning':logging.WARNING,\
'error':logging.ERROR,\
'critical':logging.CRITICAL,}
logger=logging.getLogger()
level='default'
def createFile(filename):
path=filename[0:filename.rfind('/')]
if not os.path.isdir(path):
os.makedirs(path)
if not os.path.isfile(filename):
#创建并打开一个新文件
fd = open(filename,mode='w',encoding='utf-8')
fd.close()
class MyLog:
log_filename='E:/quality/it/pyrequest-master/log/itest.log'
err_filename='E:/quality/it/pyrequest-master/log/err.log'
dateformat='%Y-%m-%d %H:%M:%S'
logger.setLevel(LEVELS.get(level,logging.NOTSET))
createFile(log_filename)
createFile(err_filename)
#注意文件内容写入时编码格式指定
handler=logging.FileHandler(log_filename,encoding='utf-8')
errhandler=logging.FileHandler(err_filename,encoding='utf-8')
@staticmethod
#静态方法
def debug(log_message):
setHandler('debug')
logger.debug("[DEBUG "+getCurrentTime()+"]"+log_message)
removerhandler('debug')
@staticmethod
def info(log_message):
setHandler('info')
logger.info("[INFO "+getCurrentTime()+"]"+log_message)
removerhandler('info')
@staticmethod
def warning(log_message):
setHandler('warning')
logger.warning("[WARNING "+getCurrentTime()+"]"+log_message)
removerhandler('warning')
@staticmethod
def error(log_message):
setHandler('error')
logger.error("[ERROR "+getCurrentTime()+"]"+log_message)
removerhandler('error')
@staticmethod
def critical(log_message):
setHandler('critical')
logger.critical("[CRITICAL "+getCurrentTime()+"]"+log_message)
removerhandler('critical')
# logger可以看做是一个记录日志的人,对于记录的每个日志,他需要有一套规则,比如记录的格式(formatter),
# 等级(level)等等,这个规则就是handler。使用logger.addHandler(handler)添加多个规则,
# 就可以让一个logger记录多个日志。
def setHandler(level):
if level=='error':
logger.addHandler(MyLog.errhandler)
#handler=logging.FileHandler(log_filename)
#把logger添加上handler
logger.addHandler(MyLog.handler)
def removerhandler(level):
if level=='error':
logger.removeHandler(MyLog.errhandler)
logger.removeHandler(MyLog.handler)
def getCurrentTime():
return time.strftime(MyLog.dateformat,time.localtime(time.time()))
if __name__=="__main__":
MyLog.debug("This is debug message")
MyLog.info("This is info message")
MyLog.warning("This is warning message")
MyLog.error("This is error message")
MyLog.critical("This is critical message")
来源:https://blog.csdn.net/zhengshengnan123/article/details/71191814
0
投稿
猜你喜欢
- 问题一:安装模块时出现报错 Microsoft Visual C++ 14.0 is required,也下载安装了运行库依然还是
- CalStatistics.pydef getNum(): #获取用户不定长度的输入
- 写作思路1、简述实现原理2、部分代码解析3、位置同步解析(①上下两屏位置同步②编辑屏位置保持不变)效果图如下:版本1:这就是我们常见的预览窗
- 一、什么是星号变量最初,星号变量是用在函数的参数传递上的,在下面的实例中,单个星号代表这个位置接收任意多个非关键字参数,在函数的*b位置上将
- 一、文章概述本文将要讲述的是Python环境下如何用OpenCV检测人脸,本文的主要内容分为:1、检测图片中的人脸2、实时检测视频中出现的人
- 一、batch与shell中目录及文件:C:\TESTFOLDER\TEST├─Test2└─Test3
- 导入模块模块通常为单独的.py文件,可以用import直接引用,可以作为模块的文件类型有.py、.pyo、.pyc、.pyd、.so、.dl
- 如下所示:import cv2import os#图片路径im_dir = '/home/suanfa/data/out/20170
- 本文实例讲述了基于wxpython开发的简单gui计算器。分享给大家供大家参考。具体如下:# wxCalc1 a simple GUI ca
- 很多组织机构慢慢的在不同的服务器和地点部署SQL Server数据库——为各种应用和目的&m
- 事件对象asyncio.Event是基于threading.Event来实现的。事件可以一个信号触发多个协程同步工作,例子如下:import
- 概要:Oracle关系数据库系统以其卓越的性能获得了广泛的应用,而保证数据库的安全性 是数据库管理工作的重要内容。本文是笔者在总结Oracl
- #!/usr/local/bin/php -q author:freemouse <?php // 下面是说明. print (&qu
- 本文实例讲述了python实现计算资源图标crc值的方法,分享给大家供大家参考。具体方法如下:实现该功能的关键在于解析资源信息,找到icon
- 查看某一列中有多少中取值:数据集名.drop_duplicates(['列名'])#实际为删除重复项,删除后对原数据集不修改
- 在上章节讲述到图像特征检测与匹配 ,本章节是讲述目标检测与识别。后者是在前者的基础上进一步完善。在本章中,我们使用HOG算法,HOG和SIF
- 将转储设备加入到SQL Server备份数据库的地方。在SEM中转储设备是可见性的,并且在设备上的信息被存储在主要数据库的sysdevice
- 永远不要相信用户输入的内容具有适当的大小或者包含适当的字符。在使用其做出决策之前应该始终对用户输入进行验证。最佳的选择是创建一个
- 近期在做爬虫时有时会遇到网站只提供pdf的情况,这样就不能使用scrapy直接抓取页面内容了,只能通过解析PDF的方式处理,目前的解决方案大
- 匿名管道管道是一个单向通道,有点类似共享内存缓存.管道有两端,包括输入端和输出端.对于一个进程的而言,它只能看到管道一端,即要么是输入端要么