From 58e0397be1c4a953e1da3d928a979c037cb15593 Mon Sep 17 00:00:00 2001 From: Janus Date: Tue, 9 Jul 2024 16:24:42 +0800 Subject: [PATCH] feat: add Python official Logging rotating handlers support (#6167) --- jina/logging/logger.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/jina/logging/logger.py b/jina/logging/logger.py index 1d48f65cfc4c6..c4f865fafeac9 100644 --- a/jina/logging/logger.py +++ b/jina/logging/logger.py @@ -108,7 +108,7 @@ class JinaLogger: :return:: an executor object. """ - supported = {'FileHandler', 'StreamHandler', 'SysLogHandler', 'RichHandler'} + supported = {'FileHandler', 'StreamHandler', 'SysLogHandler', 'RichHandler', 'TimedRotatingFileHandler', 'RotatingFileHandler'} def __init__( self, @@ -246,6 +246,27 @@ def add_handlers(self, config_path: Optional[str] = None, **kwargs): handler = logging.FileHandler(filename, delay=True) handler.setFormatter(fmt(cfg['format'].format_map(kwargs))) + elif h == 'TimedRotatingFileHandler': + filename = cfg['filename'].format_map(kwargs) + handler = logging.handlers.TimedRotatingFileHandler( + filename=filename, + when=cfg['when'], + interval=cfg['interval'], + backupCount=cfg['backupCount'], + encoding=cfg.get('encoding', 'utf-8') + ) + handler.setFormatter(fmt(cfg['format'].format_map(kwargs))) + + elif h == 'RotatingFileHandler': + filename = cfg['filename'].format_map(kwargs) + handler = logging.handlers.RotatingFileHandler( + filename=filename, + maxBytes=cfg['maxBytes'], + backupCount=cfg['backupCount'], + encoding=cfg.get('encoding', 'utf-8') + ) + handler.setFormatter(fmt(cfg['format'].format_map(kwargs))) + if handler: self.logger.addHandler(handler)