Python fileConfig 设置的日志记录错误

Python logging error from fileConfig setup

我正在尝试使用 fileConfig 为我的 Web 服务器设置日志记录,但我在设置时遇到以下错误:

File "/home/my/project/server.py", line 32, in <module>  
    fileConfig(fname='logging.conf')  
File "/usr/lib/python3.7/logging/config.py", line 79, in fileConfig  
    handlers = _install_handlers(cp, formatters)  
File "/usr/lib/python3.7/logging/config.py", line 145, in _install_handlers  
    h = klass(*args, **kwargs)  
TypeError: __init__() missing 1 required positional argument: 'filename' 

这是我的 logging.conf 文件:

[loggers]  
keys=dev,prod  

[handlers]  
keys=consoleHandler,fileHandler  

[formatters]  
keys=full,simple  

[logger_prod]  
level=INFO  
handlers=fileHandler  
qualname=prod  

[logger_dev]  
level=DEBUG  
handlers=consoleHandler  
qualname=dev  
propagate=0  

[handler_consoleHandler]  
class=StreamHandler  
level=DEBUG  
formatter=full  
args=(sys.stdout,)  

[handler_fileHandler]  
class=handlers.RotatingFileHandler  
formatter=full  
filename='logs/server.log'  
maxBytes=1500  
backupCount=20
  
[formatter_full]  
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s  

[formatter_simple]  
format=%(asctime)s - %(message)s  

...这是我的初始化:

fileConfig(fname='logging.conf')
    
if os.getenv('FLASK_ENV') == 'development':
    app.logger = logging.getLogger('dev')
else:
    app.logger = logging.getLogger('prod')

只是添加... dev 记录器工作正常,它记录到控制台,所以只是 prod 记录器导致了问题。

我也试过用 args('logs/server.log','a',1500,20) 替换 filenamemaxBytesbackupCount 选项,就像我在一些示例中看到的那样,但我得到了这个错误:

File "/home/my/project/server.py", line 32, in <module>  
    fileConfig(fname='logging.conf')  
File "/usr/lib/python3.7/logging/config.py", line 69, in fileConfig  
    cp.read(fname)  
File "/usr/lib/python3.7/configparser.py", line 696, in read  
    self._read(fp, filename)  
File "/usr/lib/python3.7/configparser.py", line 1110, in _read  
    raise e  
configparser.ParsingError: Source contains parsing errors: 'logging.conf'  
    [line 30]: "args('logs/server.log','a',1500,20)\n"

您上次尝试时有错别字,即缺少 args 之后的 =

“有效”下方的配置,即有效但会出现另一个错误。

[loggers]
keys=dev,prod

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=full,simple

[logger_prod]
level=INFO
handlers=fileHandler
qualname=prod

[logger_dev]
level=DEBUG
handlers=consoleHandler
qualname=dev
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=full
args=(sys.stdout,)

[handler_fileHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=full
args=('logs/server.log','a',1500,20)

[formatter_full]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

[formatter_simple]
format=%(asctime)s - %(message)s

现在你会得到一个错误,因为你没有指定 root 记录器应该做什么。

请参阅 Can I have logging.ini file without root logger? 了解错误和解决方法。

您的配置文件应如下所示:

[loggers]
keys=root,dev,prod

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=full,simple

[logger_root]
level=NOTSET
handlers=consoleHandler

[logger_prod]
level=INFO
handlers=fileHandler
qualname=prod

[logger_dev]
level=DEBUG
handlers=consoleHandler
qualname=dev
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=full
args=(sys.stdout,)

[handler_fileHandler]
class=handlers.RotatingFileHandler
formatter=full
args=('logs/server.log', 'w', 1500, 20)

[formatter_full]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

[formatter_simple]
format=%(asctime)s - %(message)s

https://docs.python.org/3/library/logging.config.html#configuration-file-format

改为

[handler_fileHandler]  
class=handlers.RotatingFileHandler  
formatter=full  
filename='logs/server.log'  
maxBytes=1500  
backupCount=20

使用

[handler_fileHandler]  
class=handlers.RotatingFileHandler  
formatter=full  
args=('logs/server.log', 'w')
maxBytes=1500  
backupCount=20