python日志记录模块没有写任何文件

时间:2021-08-18 22:03:05

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.

此代码完美地记录到控制台,但没有记录到该文件。文件已创建,但没有写入任何内容。我试过关闭处理程序,但这没有做任何事情。也没有冲洗它。我搜索了互联网,但显然我是唯一有这个问题的人。有谁知道问题是什么?谢谢你的回答。

3 个解决方案

#1


20  

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')

#2


1  

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.log all logs that are of level debug or higher.

这将写入example.log所有级别调试或更高级别的日志。

logging.debug("This is a debug message") will write This is a debug message to example.log. Level is important for this to work.

logging.debug(“这是一个调试消息”)将写入这是对example.log的调试消息。水平对于此工作非常重要。

#3


1  

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 ...

#1


20  

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')

#2


1  

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.log all logs that are of level debug or higher.

这将写入example.log所有级别调试或更高级别的日志。

logging.debug("This is a debug message") will write This is a debug message to example.log. Level is important for this to work.

logging.debug(“这是一个调试消息”)将写入这是对example.log的调试消息。水平对于此工作非常重要。

#3


1  

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 ...