python实现log日志的示例代码

yipeiwu_com6年前Python基础

源代码:

# 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")
  

以上这篇python实现log日志的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

PyQt Qt Designer工具的布局管理详解

PyQt Qt Designer工具的布局管理详解

前言 这节课很重要。。界面整洁美观与否就看布局了。。这里讲布局方法,至于设计的天赋与最终界面的美感那就看造化了。。 本文主要讲述Qt Designer工具实现界面控件布局管理,就是排列组...

Python3使用turtle绘制超立方体图形示例

Python3使用turtle绘制超立方体图形示例

本文实例讲述了Python3使用turtle绘制超立方体图形。分享给大家供大家参考,具体如下: 利用Python3中turtle的绘制超立方体。 绘图思路: 1)求出边长100的超立方...

Python使用wxPython实现计算器

本文实例为大家分享了wxPython实现计算器的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- ########################...

python友情链接检查方法

本文实例讲述了python友情链接检查方法。分享给大家供大家参考。具体实现方法如下: # _*_ coding:utf-8 _*_ #xiaohei.python.seo.call....

Python的Urllib库的基本使用教程

Python的Urllib库的基本使用教程

1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它 是一段HTML代码,...