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


猜你喜欢
- 楔子在介绍数据类型的时候我们说过,Python 的数据类型相比 C 来说要更加的通用,但速度却远不及 C。如果你在使用 Cython 加速
- MySQL数据库是一款非常好用的数据库管理系统,但是相对来说卸载起来麻烦一些这里给大家分享下MySQL数据库如何卸载干净~1 停止MySQL
- 加载相关库import randomimport seaborn as snsimport matplotlib.pyplot as plt
- python查找图片按钮的坐标位置因为业务需求,了解到一些python自动化的内容,其中有类似于airtest截图点击的东西。本来是想使用p
- css+div做的菜单:一个主显示层,别的列表都隐藏着,用js函数设置列表的显示和隐藏。分别用到了两个函数,函数实现的效果是一样的,一个是参
- 首先建一个access 数据库,库中有一个URLINDEX表,其中URL和Keywords字段分别添加了索引,如下:URL &nb
- 本文实例讲述了Yii开启片段缓存的方法。分享给大家供大家参考,具体如下:1、main.php文件中的components中添加:cache&
- 查询语言通过在查询表格中键入单词或短语,然后单击按钮执行查询,就可以在 Web 站点中搜索任意的单词或短语(例如,查询表格示例
- SQLServer数据导出到excel有很多种方法,比如dts、ssis、还可以用sql语句调用openrowset。我们这里开拓思路,用C
- 1. 安装Pyechartspip install pyecharts2. 图表基础2.1 主题风格添加主题风格使用的是 InitOpts(
- def cndebug(obj=False): """ Author : Nemon Update : 200
- Python 的 Queue 模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列Lifo
- 前言在前一篇文章中分享了编译器优化的变量捕获部分,本文分享编译器优化的另一个内容—函数内联。函数内联是指将将较小的函数内
- 最近我因为要安装 Firebug 1.4 导致我不得不安装了 Firefox 3.5 ,所以很不小心地接触到了Wordpress后台那漂亮的
- Python的创始人为荷兰人吉多·范罗苏姆 (Guido van Rossum)。 * 圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞
- 1、说明写了python文件,想通过命令执行它,进入目录,并在命令执行中输入python文件名称。2、实例例如,桌子上有文件hello.py
- 目录Java的数据库连接(JDBC)1、什么是JDBC2、JDBC的原理3、演示JDBC的使用4、数据库连接方式5、JDBC的查询6、SQL
- 一、介绍我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某
- 想使用正则表达式来获取一段文本中的任意字符,写出如下匹配规则: (.*) 结果运行之后才发现,无法获得换行之后的文本。于是查了一下手册,才发
- hashlib 模块hashlib 模块的介绍hashlib 模块中拥有很多的加密算法,我们并不需要关心加密算法的实现方法。只需要调用我们需