• python-记log


    import traceback
    import os
    from datetime import datetime
    
    
    def wrtLog(funName):
        def func_in(*args, **kwargs):
            try:
                return funName(*args, **kwargs)  # 返回值
            except:
                print(traceback.format_exc())
                saveLog(traceback.format_exc())
        return func_in
    
    
    def saveLog(errMsg):
        try:
            if(not os.path.exists("C:\RecFile")):
                os.makedirs("C:\RecFile")
            with open("C:\RecFile\errLog.txt", 'a', encoding='utf8', errors='ignore') as fa:
                fa.write("
    记录时间:%s" % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
                fa.write("
    -----------------------------------------------------------------------------
    ")
                fa.write(errMsg)
                fa.write("
    =============================================================================
    ")
        except:
            print(traceback.format_exc())

    调用示例,

    @是函数修饰符,是将下面的函数作为参数传到@后面的函数。

    from writeLog import wrtLog
    
            
    @wrtLog
    def  div(a,b):
        c=a/b
        return c
    
    
    c=div(7,0)
  • 相关阅读:
    git 常用命令
    最近任务 && react文章列表
    markdown 常用格式API
    webpack 插件怎么写
    iframe
    重置浏览器滚动条css
    webpack4
    vscode插件开发
    vscode插件
    react router 的push replace
  • 原文地址:https://www.cnblogs.com/JinweiChang/p/12448973.html
Copyright © 2020-2023  润新知