Skip to content

Commit

Permalink
feat: add Python official Logging rotating handlers support (#6167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janus-Xu authored Jul 9, 2024
1 parent 53ec704 commit 58e0397
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion jina/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 58e0397

Please sign in to comment.