2016-11-08 33 views
0

当我使用logging.config.dictConfig设置我的日志时,如何翻转我的日志?我无法检索我的logging.handlers.RotatingFileHandler访问处理程序对象到翻转日志

import logging 
import logging.config 


logging.config.dictConfig({ 
    'version': 1, 
    'disable_existing_loggers': False, 
    'handlers': { 
     'default': { 
      'class':'logging.handlers.RotatingFileHandler', 
      "level": "DEBUG", 
      "filename": "test.log", 
      'backupCount': 5, 
      'maxBytes': 20, 
     }, 
    }, 
    'loggers': { 
     '': { 
      'handlers': ['default'], 
      'level': 'DEBUG', 
      'propagate': True 
     } 
    } 
}) 

# How do I do rollover?? 
logging.handler[0].doRollover() 

logging.info("foo") 

回答

0

您可以访问使用logging.getLogger().handlers根处理程序,所以如果你只有一个,你会做这样的事情

for h in logging.getLogger().handlers: 
    if type(h) == logging.handlers.RotatingFileHandler: 
     h.doRollover()