Created
February 9, 2018 09:24
-
-
Save timedcy/87f589bc505e2524de785db246530afe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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