python实现log日志的示例代码

yipeiwu_com5年前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设计】。

相关文章

Python enumerate函数功能与用法示例

本文实例讲述了Python enumerate函数功能与用法。分享给大家供大家参考,具体如下: eunmerate在英文中是列举、枚举的意思,在python中eunmerate()是一个...

python操作excel让工作自动化

某局某领导给了3只excel文件,一只里面有4个sheet需要处理,一个sheet有250+列,算下来总共有3000+列需要手动反复插入、删除列、拷贝、求和,所以给了4天的时间要完成。...

Python实现替换文件中指定内容的方法

本文实例讲述了Python实现替换文件中指定内容的方法。分享给大家供大家参考,具体如下: 这里使用python编写的程序,实现如下功能:将文件中的指定子串 修改为 另外的子串 编写的py...

python中多个装饰器的执行顺序详解

python中多个装饰器的执行顺序详解

装饰器是程序开发中经常会用到的一个功能,也是python语言开发的基础知识,如果能够在程序中合理的使用装饰器,不仅可以提高开发效率,而且可以让写的代码看上去显的高大上^_^ 使用场景...

一步步解析Python斗牛游戏的概率

过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率)。 斗牛的玩法是:...