喜歡這款無腦操作的 神logging
並且無須變更就很美的 log
先看看效果
from loguru import logger as logur
logur.debug('this is debug')
logur.info('this is info')
logur.warning('this is warning')
logur.error('this is error')
logur.critical('this is critical')
執行後顯示

如果要記錄 log 檔案
from loguru import logger as logur
logur.add('%s_{time}.log' % __file__, level="ERROR", diagnose=True, backtrace=True, encoding="utf8")
logur.debug('this is debug')
logur.info('this is info')
logur.warning('this is warning')
logur.error('this is error')
logur.critical('this is critical')
# 官方 意識的 trace
def func(a, b):
return a / b
def nested(c):
try:
func(5, c)
except ZeroDivisionError:
logur.exception("What?!")
然後呢,打開 log 就會連同 trace 訊息也記錄囉
2021-06-17 01:51:23.468 | ERROR | __main__:loguru:49 - this is error
2021-06-17 01:51:23.469 | CRITICAL | __main__:loguru:50 - this is critical
2021-06-17 01:51:23.469 | ERROR | __main__:nested:42 - What?!
Traceback (most recent call last):
File "U:/PY/practice/ts_logger.py", line 57, in <module>
nested(0)
└ <function nested at 0x0000016589E969D0>
> File "U:/PY/practice/ts_logger.py", line 40, in nested
func(5, c)
│ └ 0
└ <function func at 0x0000016589E96940>
File "U:/PY/practice/ts_logger.py", line 35, in func
return a / b
│ └ 0
└ 5
ZeroDivisionError: division by zero
很方便的~ 超級推薦