Skip to content

Instantly share code, notes, and snippets.

@mickhan
Created June 5, 2013 03:59
Show Gist options
  • Save mickhan/5711515 to your computer and use it in GitHub Desktop.
Save mickhan/5711515 to your computer and use it in GitHub Desktop.
记录文件日志,每天midnight创建新文件
import config
import logging
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger(config.LOG_NAME)
logfile = '%s/%s' % (config.LOG_BASE_PATH, config.LOG_FILENAME)
fileTimeHandler = TimedRotatingFileHandler(logfile, "midnight", 1)
fileTimeHandler.suffix = "%Y%m%d.log" # 设置切分后日志文件名格式
logging.basicConfig(level=logging.INFO)
formatter = logging.Formatter(
'%(asctime)s %(levelname)s %(filename)s %(message)s') # 日志格式
fileTimeHandler.setFormatter(formatter)
logger.addHandler(fileTimeHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment