python 日志函数参数_将日志信息作为参数传递给函数
我使用的Python庫使用logging模塊。但是,我創建了自己的log函數,腳本在內部使用。在
下面是我要使用的日志記錄功能:def log(name, content, swtch : bool = None, time = None):
time = time or datetime.now(pytz.timezone('US/Pacific'))
if swtch == True or swtch == None:
toPrint = '{1.RED}{2}/{3}/{4} {5}:{6}:{7}:{8} {9} {0.BRIGHT}{1.GREEN}{10} {0.RESET_ALL}{11}{0.RESET_ALL}'.format(
Style,
Fore,
str(time.month).zfill(2),
str(time.day).zfill(2),
str(time.year)[2:],
str(time.hour % 12).zfill(2),
str(time.minute).zfill(2),
str(time.second).zfill(2),
str(int(time.microsecond / 1000)).zfill(3),
'AM' if time.hour < 12 else 'PM',
name,
content
)
print(toPrint)
log_txt = ''
if swtch == False or swtch == None:
file = open('log.txt', 'r')
log_txt = file.read()
file.close()
with open('log.txt', 'w') as file:
text = '{0}/{1}/{2} {3}:{4}:{5}:{6} {7} {8} {9}'.format(
str(time.month).zfill(2),
str(time.day).zfill(2),
str(time.year)[2:],
str(time.hour % 12).zfill(2),
str(time.minute).zfill(2),
str(time.second).zfill(2),
str(int(time.microsecond / 1000)).zfill(3),
'AM' if time.hour < 12 else 'PM',
name,
content
)
file.write(log_txt + text + '\n')
讓我們假設有一個名為some_logger的記錄器。在
^{pr2}$
有沒有辦法代替打印到stdout,而是將位置參數(表示日志信息)傳遞給另一個函數?(為了澄清:一個將使用所需參數調用log的函數)
總結
以上是生活随笔為你收集整理的python 日志函数参数_将日志信息作为参数传递给函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国宣布美元和黄金脱钩的时间是多久?
- 下一篇: python的zip方法_python