Python-Loguru日志库

Loguru is a library which aims to bring enjoyable logging in Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# logger
from loguru import logger
logger.info("This is log info!")
logger.warning("This is log warn!")
logger.error("This is log error!")
logger.debug("This is log debug!")

# add()
logger.add("file_{time}.log") # 保存文件
logger.add("file_1.log", rotation="500 MB") # Automatically rotate too big file
logger.add("file_2.log", rotation="12:00") # New file is created each day at noon
logger.add("file_3.log", rotation="1 week") # Once the file is too old, it's rotated
logger.add("file_X.log", retention="10 days") # Cleanup after some time
logger.add("file_Y.log", compression="zip") # Save some loved space
logger.add("file.log", format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}")
logger.add(sys.stdout, colorize=True, format="<green>{time}</green> <level>{message}</level>")

# remove

# 字符串格式化
logger.info('If you are using Python {}, prefer {feature} of course!', 3.6, feature='f-strings')

# 进程安全
logger.add("somefile.log", enqueue=True) # 还支持用作接收器的协程函数,应该等待complete()

# 异常追溯
@logger.catch # 装饰器
logger.exception("What?!")