我试图弄清楚如何在运行我的脚本时捕获由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
答案 0 :(得分:0)
Afaik,您必须在basicConfig中指定,而不是在记录器中指定:
logging.basicConfig(filename=LOG_FILE,
level=logging.DEBUG)
在之前
logger = logging.getLogger('MyApp')
logger.setLevel(logging.DEBUG)