记录Python / NumPy控制台消息

时间:2017-02-19 17:46:54

标签: python numpy

我试图弄清楚如何在运行我的脚本时捕获由Python / NumPy intractive shell生成的消息。我想将控制台消息(错误,警告)生成的所有内容记录到我的代码日志消息中定义的相同文件中,并带有时间戳:

def LogToFile():
 global logger
 logger = logging.getLogger('MyApp')
 logger.setLevel(logging.DEBUG)
 file_log_handler = RotatingFileHandler('logfile.log', maxBytes=1024, backupCount=5)
 logger.addHandler(file_log_handler)
 stderr_log_handler = logging.StreamHandler()
 logger.addHandler(stderr_log_handler)
 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 file_log_handler.setFormatter(formatter)
 stderr_log_handler.setFormatter(formatter)
 return logger

1 个答案:

答案 0 :(得分:0)

Afaik,您必须在basicConfig中指定,而不是在记录器中指定:

logging.basicConfig(filename=LOG_FILE,
                    level=logging.DEBUG)
在之前

logger = logging.getLogger('MyApp')

logger.setLevel(logging.DEBUG)