Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在rotatingfilehandler中查看文件名更改?_Python_Logging - Fatal编程技术网

Python 如何在rotatingfilehandler中查看文件名更改?

Python 如何在rotatingfilehandler中查看文件名更改?,python,logging,Python,Logging,我使用gunicorn运行flask应用程序的2个进程。 我使用filerotatingfilehandler来旋转日志文件。 问题是当一个进程旋转日志文件时,另一个进程不知道,然后它再次旋转该文件(现在名为log.txt.1)(到log.txt.2,可能会将log.txt更改为log.txt.1)。然后一个进程登录到log.txt,而另一个进程登录到log.txt.1。 我知道我可以使用watchedfilehandler+logrotate来解决这个问题。 但是我仍然可以使用rotating

我使用gunicorn运行flask应用程序的2个进程。 我使用filerotatingfilehandler来旋转日志文件。 问题是当一个进程旋转日志文件时,另一个进程不知道,然后它再次旋转该文件(现在名为log.txt.1)(到log.txt.2,可能会将log.txt更改为log.txt.1)。然后一个进程登录到log.txt,而另一个进程登录到log.txt.1。 我知道我可以使用watchedfilehandler+logrotate来解决这个问题。
但是我仍然可以使用rotatingfilehandler并检测文件名更改吗?

您可以使用分布式全局锁来防止双重旋转,如

但是,我建议在您的Flask应用程序中使用以下内容:

来自芹菜。设置后导入信号\u记录器
从日志导入错误,格式化程序
从logging.handlers导入WatchedFileHandler
从yourapp导入应用程序
日志格式=(
'-'*80+'\n'+
“%(模块)s[%(路径名)s:%(行号)d]中的%(levelname)s:\n”+
“%(消息)s\n”+
'-' * 80
)
如果不是app.config['DEBUG']:
error\u handler=WatchedFileHandler(app.config['error\u handler\u LOG'],delay=True)
错误\处理程序.setFormatter(格式化程序(日志\格式))
错误\u handler.setLevel(错误)
@安装后\u logger.connect
设置后的def记录器(记录器,*args,**kwargs):
logger.addHandler(错误处理程序)
@第一次请求前的应用程序
第一次请求之前的定义()
app.logger.addHandler(错误处理程序)
然后使用cron脚本或文件监视守护程序通过重命名日志文件来旋转日志文件