• 日志


        在跑脚本时,之前一直用第三方库logging,但运行的时候时好时坏,经常没有写进日志进去,因为去看源码和帮助文档,觉得挺麻烦的,就干脆自己写代码封装了一个日志的功能(自己写的比较清楚使用),

    实现了最基本的功能:

    1、可参数化配置写入日志文件的等级(debug、info、error三个等级)

    2、可配置日志文件path

    功能由三个文件组成,一个是生成日志文件或者写入日志文件类方法,解析配置文件设置的日志水平;一个是调用写入日志文件的函数,判断当前日志是否大于设置的日志水平;还有一个是配置文件。

    配置文件配置如下:

    类方法代码示下:

    from utils.getDate import *
    from utils.getRootPath import *
    from config.logConf import *

    class weLog(object):
    levelList=["debug","info","error"]
    def __init__(self,SetLevel,logfilePath):
    self.logfilePath=logfilePath.strip("\")
    if isinstance(SetLevel,int):
    if SetLevel>= 0 and SetLevel<=2:
    self.SetLevel=SetLevel
    else:
    self.SetLevel =1
    if isinstance(SetLevel, str):
    if SetLevel.lower() in self.levelList:
    self.SetLevel=self.levelList.index(SetLevel.lower())
    else:
    self.SetLevel = 1
    else:
    self.SetLevel = 1

    def writeLogsInFile(self,level,log_content):
    if os.path.exists(self.logfilePath):
    if os.path.exists(self.logfilePath + '\' + getDate.getDate() + '.log'):
    file = open(self.logfilePath + '\' + getDate.getDate() + '.log', "r", encoding='utf-8')
    line_content = file.readline()
    if line_content.strip() == '':
    file.close()
    with open(self.logfilePath + '\' + getDate.getDate() + '.log', 'a+',
    encoding='utf-8') as fq:
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)
    else:
    file.close()
    with open(self.logfilePath + '\' + getDate.getDate() + '.log', 'a+',
    encoding='utf-8') as fq:
    fq.write(" ")
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)
    else:
    file = open(self.logfilePath + '\' + getDate.getDate() + '.log', "w",
    encoding='utf-8') # 创建文件
    file.close()
    with open(self.logfilePath + '\' + getDate.getDate() + '.log', 'r+',
    encoding='utf-8') as fq:
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)
    else:
    if os.path.exists(getRootPath(getRootPath())+'\logs\'+getDate.getDate()+'.log'):
    file = open(getRootPath(getRootPath()) + '\logs\' + getDate.getDate() + '.log', "r",encoding='utf-8')
    line_content=file.readline()
    if line_content.strip() == '':
    file.close()
    with open(getRootPath(getRootPath())+'\logs\'+getDate.getDate()+'.log','a+',encoding='utf-8') as fq:
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)
    else:
    file.close()
    with open(getRootPath(getRootPath()) + '\logs\' + getDate.getDate() + '.log', 'a+', encoding='utf-8') as fq:
    fq.write(" ")
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)
    else:
    file=open(getRootPath(getRootPath())+'\logs\'+getDate.getDate()+'.log',"w",encoding='utf-8') #创建文件
    file.close()
    with open(getRootPath(getRootPath())+'\logs\'+getDate.getDate()+'.log','r+',encoding='utf-8') as fq:
    fq.write(getDate.getDate() + ' ' + getDate.getTime() + ' WELOG '+level.upper()+" ")
    fq.write(log_content)


    调用函数如下:
    from utils.log.weLog import *
    def excutelog(level, logcontent):
    logwriter = weLog(SetLevel, logfilePath)
    if level.lower() in logwriter.levelList:
    if logwriter.levelList.index(level.lower()) >= logwriter.SetLevel:
    logwriter.writeLogsInFile(level,logcontent)
    else:
    pass
    else:
    print("input loglevel unknows ")

    if __name__=="__main__":
    excutelog("debug","debug testing")
    excutelog("info", "this is whiteMouse write logs")
    excutelog("error", "input type error")


    运行结果如下

  • 相关阅读:
    poj3417 闇の連鎖 【树上差分】By cellur925
    Luogu P1613跑路【倍增】By cellur925
    CF519E A and B and Lecture Rooms
    poj 2412 The Balance 【exgcd】By cellur925
    NOIp 2014 解方程 【数学/秦九韶算法/大数取膜】By cellur925
    Maven项目整合SSH框架
    传递依赖
    Maven项目整合Struts2框架
    K.O. -------- Eclipse中Maven的报错处理
    依赖范围
  • 原文地址:https://www.cnblogs.com/whitemouseV2-0/p/12627205.html
Copyright © 2020-2023  润新知