Skip to content

Instantly share code, notes, and snippets.

@timedcy
Created February 9, 2018 09:24
Show Gist options
  • Save timedcy/87f589bc505e2524de785db246530afe to your computer and use it in GitHub Desktop.
Save timedcy/87f589bc505e2524de785db246530afe to your computer and use it in GitHub Desktop.
"""
Created on 2017-02-28
@author: timedcy@gmail.com
"""
import logging
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
def create_timed_rotating_log(filename, when="d", interval=1, backup_count=8, encoding='utf8', level=logging.INFO, with_console=False):
""""""
fmt = '%(asctime)s:%(levelname)s:%(funcName)s:%(lineno)d: %(message)s'
logger = logging.getLogger("TimedRotatingLog")
logger.setLevel(level)
fn = str(filename) if isinstance(filename, Path) else filename
handler = TimedRotatingFileHandler(filename=fn,
when=when,
interval=interval,
backupCount=backup_count,
encoding=encoding)
handler.setFormatter(logging.Formatter(fmt))
logger.addHandler(handler)
if with_console:
console = logging.StreamHandler()
console.setLevel(level)
console.setFormatter(logging.Formatter(fmt))
logger.addHandler(console)
return logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment