python日志记录模块没有向文件写入任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15892946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python logging module is not writing anything to file
提问by thePurpleMonkey
I'm trying to write a server that logs exceptions both to the console and to a file. I pulled some code off the cookbook. Here it is:
我正在尝试编写一个将异常记录到控制台和文件的服务器。我从食谱中提取了一些代码。这里是:
logger = logging.getLogger('server_logger')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('server.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
This code logs perfectly fine to the console, but nothing is logged to the file. The file is created, but nothing is ever written to it. I've tried closing the handler, but that doesn't do anything. Neither does flushing it. I searched the Internet, but apparently I'm the only one with this problem. Does anybody have any idea what the problem is? Thanks for your answers.
这段代码很好地记录到控制台,但没有记录到文件中。文件已创建,但没有写入任何内容。我试过关闭处理程序,但这没有任何作用。冲洗它也不行。我搜索了互联网,但显然我是唯一一个遇到这个问题的人。有人知道问题是什么吗?感谢您的回答。
采纳答案by Leopd
Try calling
试试打电话
logger.error('This should go to both console and file')
instead of
代替
logging.error('this will go to the default logger which you have not changed the config of')
回答by Abass Sesay
I know that this question might be a bit too old but I found the above method a bit of an overkill. I ran into a similar issue, I was able to solve it by:
我知道这个问题可能有点过时了,但我发现上述方法有点矫枉过正。我遇到了类似的问题,我能够通过以下方式解决它:
import logging
logging.basicConfig(format = '%(asctime)s %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p',
filename = 'example.log',
level=logging.DEBUG)
This will write to example.logall logs that are of level debug or higher.
这将写入example.log所有级别为 debug 或更高级别的日志。
logging.debug("This is a debug message")will write This is a debug messageto example.log. Level is important for this to work.
logging.debug("This is a debug message")会写信This is a debug message给example.log。水平对于这个工作很重要。
回答by Fernando Benito
Try to put the import and the basicConfig at the very beggining of the script. Something like this:
尝试将 import 和 basicConfig 放在脚本的开头。像这样的东西:
import logging
logging.basicConfig(filename='log.log', level=logging.INFO)
.
.
import ...
import ...
回答by P Moran
Put this
把这个
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
in front of the
在......面前
logging.basicConfig(...)
see also Logging module not writing to file
另见 记录模块未写入文件

