i use peewee for mysql.
then peewee logging by itself
i also use coloredlogs for logging
# 开启日志
logger = logging.getLogger('peewee')
coloredlogs.install(level=logging.DEBUG, logger=logger)
can i use loguru for peewee logging?
Hi @visonforcoding.
This is totally possible but this requires a little bit more boilerplate than using coloredlogs, because Loguru doesn't rely on standard logging.
First, you need to create a special Handler in charge of intercepting logs from peewee and redistributing them to Loguru's logger:
class InterceptHandler(logging.Handler):
def emit(self, record):
# Retrieve context where the logging call occurred, this happens to be in the 6th frame upward
logger_opt = logger.opt(depth=6, exception=record.exc_info)
logger_opt.log(record.levelno, record.getMessage())
Then, you need to add it to the peewee logger as follow:
logging.getLogger("peewee").setLevel(10)
logging.getLogger("peewee").addHandler(InterceptHandler())
That way, peewee logs messages will be propagated to handlers added to Loguru. :+1:
tks! i will try it.
Most helpful comment
Hi @visonforcoding.
This is totally possible but this requires a little bit more boilerplate than using
coloredlogs, becauseLogurudoesn't rely on standardlogging.First, you need to create a special
Handlerin charge of intercepting logs frompeeweeand redistributing them to Loguru's logger:Then, you need to add it to the
peeweelogger as follow:That way,
peeweelogs messages will be propagated to handlers added toLoguru. :+1: