Python 为什么不是';我的日志代码不能工作吗?
我正在学习一个关于日志记录的教程,并尝试使用一些示例代码:Python 为什么不是';我的日志代码不能工作吗?,python,logging,Python,Logging,我正在学习一个关于日志记录的教程,并尝试使用一些示例代码: log_this = logging.getLogger("Sample") log_this.setLevel(logging.INFO) the_f = logging.FileHandler("sample.log") form = logging.Formatter("%(asctime)s (%name)s %(levelname)s %(message)s") the_f.setFormatter(form) log_
log_this = logging.getLogger("Sample")
log_this.setLevel(logging.INFO)
the_f = logging.FileHandler("sample.log")
form = logging.Formatter("%(asctime)s (%name)s %(levelname)s %(message)s")
the_f.setFormatter(form)
log_this.addHandler(the_f)
log_this.warning("hi")
只有在不包括以下内容的情况下,它才起作用:
log_this.addHandler(the_f)
你知道怎么回事吗?你的代码出现拼写错误,将
(%name)s
更改为(%name)s
,效果良好:
form = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
the_f.setFormatter(form)
log_this.addHandler(the_f)
log_this.warning('debug message')
log_this.info('info message')
输出:
2017-04-27 .. Sample WARNING debug message
2017-04-27 .. Sample INFO info message
当你把
log\u this.addHandler(the\u f)
行放到文件中时,它被记录了吗?这是一个。