# log.py
from loguru import logger
# filter
def info_only(record: dict) -> bool:
"""
Other levels are filtered out
:param record: The record is just a Python dict, accessible from sinks by message.record.
It contains all contextual information of the logging call (time, function, file, line, level, etc.).
:return:
"""
return record["level"].name == "INFO"
logger.add(
sink="info.log",
level="INFO",
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {file} - [line:{line}] - {message}",
filter=lambda record: record["level"].name == "INFO", # Other levels are filtered out
rotation="50 MB",
retention=5,
)
logger.add(
sink="error.log",
level="ERROR",
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {file} - [line:{line}] - {message}",
rotation="50 MB",
retention=5,
)
参考链接
https://loguru.readthedocs.io/en/stable/index.html
https://github.com/Delgan/loguru