python程序日志脚本,一个方法使用

日志级别

日志一共分成5个等级,从低到高分别是:

·DEBUG
·INFO
·WARNING
·ERROR
·CRITICAL

日志级别说明:

DEBUG:详细的信息,通常只出现在诊断问题上
INFO:确认一切按预期运行
WARNING:一个迹象表明,一些意想不到的事情发生了,或表明一些问题在不久的将来(例如。磁盘空间低”)。这个软件还能按预期工作。
ERROR:更严重的问题,软件没能执行一些功能
CRITICAL:一个严重的错误,这表明程序本身可能无法继续运行

这5个等级,也分别对应5种打日志的方法: debug 、info 、warning 、error 、critical。默认的是WARNING,当在WARNING或之上时才被跟踪。

代码使用示例

#导入脚本
import Inset_Diary
#三种方法写日志。可以自己扩展
Inset_Diary.Inset_Into('这个功能即将写日志')
Inset_Diary.Inset_Into('日志第一行')

Inset_Diary.Inset_Debug('这个功能即将写Bug日志')
Inset_Diary.Inset_Debug('BUG日志第一行:')

Inset_Diary.Inset_error('这个功能即将写错误日志')
Inset_Diary.Inset_error('错误日志第一行:')

效果展示

图片结果展示

代码实例,命名【Inset_Diary.py】

import time,os

SFXR=1
def createFile(filePath):
    if os.path.exists(filePath):
        #print('%s:存在'%filePath)
        pass
    else:
        try:
            os.mkdir(filePath)
            #print('新建文件夹:%s'%filePath)
        except Exception as e:
            os.makedirs(filePath)
           # print('新建多层文件夹:%s' % filePath)
def Inset_Into(strs):
    # 定义时间
    sys_time = time.time()
    sys_time_array = time.localtime(sys_time)
    current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array)
    createFile('./Log/')
    f = './Log/Into_'+time.strftime("%Y-%m-%d",sys_time_array)+'.txt'
    if SFXR==1:
        with open(f,"a") as file:
            file.write('【'+current_time+'】:'+strs+"\n")

def Inset_Debug(strs):
    # 定义时间
    sys_time = time.time()
    sys_time_array = time.localtime(sys_time)
    current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array)
    createFile('./Log/')
    f = './Log/Debug_'+time.strftime("%Y-%m-%d",sys_time_array)+'.txt'
    if SFXR==1:
        with open(f,"a") as file:
            file.write('【'+current_time+'】:'+strs+"\n")

def Inset_error(strs):
    # 定义时间
    sys_time = time.time()
    sys_time_array = time.localtime(sys_time)
    current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array)
    createFile('./Log/')
    f = './Log/error_'+time.strftime("%Y-%m-%d",sys_time_array)+'.txt'
    if SFXR==1:
        with open(f,"a") as file:
            file.write('【'+current_time+'】:'+strs+"\n")

def Inset_Debug_df(df,str):
    createFile('./Log/')
    f = f'./Log/Debug_{str}.csv'
    if SFXR==1:
        df.to_csv(f)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值