程序日志脚本,一个方法使用
日志级别
日志一共分成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)