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

相关文章

django中间键重定向实例方法

1,定义和注册中间件 在注册的中间件中使用: from django.http import HttpResponseRedirect '''下面的书写方法会陷入死循环,所以必须加判...

Python iter()函数用法实例分析

本文实例讲述了Python iter()函数用法。分享给大家供大家参考,具体如下: python中的迭代器用起来非常灵巧,不仅可以迭代序列,也可以迭代表现出序列行为的对象,例如字典的键、...

对DJango视图(views)和模版(templates)的使用详解

视图 在django中,视图对WEB请求进行回应 视图接收reqeust对象作为第一个参数,包含了请求的信息 视图就是一个Python函数,被定义在views.py中 定义完成视图后,需...

tensorflow tf.train.batch之数据批量读取方式

在进行大量数据训练神经网络的时候,可能需要批量读取数据。于是参考了这篇文章的代码,结果发现数据一直批量循环输出,不会在数据的末尾自动停止。 然后发现这篇博文说slice_input_pr...

解决Python的str强转int时遇到的问题

数字字符串前后有空格没事: >>> print(int(" 3 ")) 3 但是下面这种带小数点的情况是不可取的: >>> print(in...