gunicorn accesslog 为空的一种可能解决办法
2020-03-12 18:39
176 查看
问题
在 gunicorn 的配置文件中,有
accesslog和
errorlog两项,分别用来记录接口的访问历史和服务启动以及错误消息。
假设有部分配置文件如下:
loglevel = 'info' accesslog = "log/gunicorn_access.log" errorlog = "log/gunicorn_error.log"
正常来说服务启动并有相应请求后,应该在
log目录下应该有非空的
gunicorn_access.log和
gunicorn_error.log。但是有时我们会发现这两个文件确实存在,只不过
gunicorn_access.log是空的,而
gunicorn_error.log正常。
WHY?
当然这里原因众多,我这里只是提出一种可能的原因和解决方案:Python 的
logging模块导致的。
分析
我们通常会在
app.py及其他程序中使用
logging来记录必要信息,在配置
logging模块时,有可能会使用
logging.config.fileConfig或者
logging.config.dictConfig,这通常会从一个配置文件中来读取配置。但是其中有个坑,官方也提到了。根据 Python
logging官方文档:
Warning The
fileConfig()function takes a default parameter,disable_existing_loggers, which defaults toTruefor reasons of backward compatibility. This may or may not be what you want, since it will cause any non-root loggers existing before thefileConfig()call to be disabled unless they (or an ancestor) are explicitly named in the configuration. Please refer to the reference documentation for more information, and specifyFalsefor this parameter if you wish.The dictionary passed to
dictConfig()can also specify a Boolean value with keydisable_existing_loggers, which if not specified explicitly in the dictionary also defaults to being interpreted asTrue. This leads to the logger-disabling behaviour described above, which may not be what you want - in which case, provide the key explicitly with a value ofFalse.
简单来说,就是无论使用
fileConfig()还是
dictConfig(),都会有个默认值为
True的参数:
disable_existing_loggers,这会导致在调用
fileConfig()或者
dictConfig()之前的非 root 的 logger 失效。
OK,那么这和 gunicorn 有什么关系呢?
gunicorn 实际上也是使用的
logging模块。在真正的启动 flask 服务之前,gunicorn 会先启动,并输出类似如下的信息到
gunicorn_error.log:
[2014-09-10 10:22:28 +0000] [30869] [INFO] Listening at: http://127.0.0.1:8000 (30869) [2014-09-10 10:22:28 +0000] [30869] [INFO] Using worker: sync [2014-09-10 10:22:28 +0000] [30874] [INFO] Booting worker with pid: 30874 [2014-09-10 10:22:28 +0000] [30875] [INFO] Booting worker with pid: 30875 [2014-09-10 10:22:28 +0000] [30876] [INFO] Booting worker with pid: 30876 [2014-09-10 10:22:28 +0000] [30877] [INFO] Booting worker with pid: 30877
此时真正的 flask 服务还未启动,也就是
app.py中的
fileConfig()或者
dictConfig()还未执行,此时的非 root logger 还未被禁用,所以
gunicorn_error.log是正常的。但是随着 flask 服务真正启动之后,gunicorn 的 logger 就被禁用了,后续新请求也就没能够记录,从而导致
gunicorn_access.log是空的。
解决
那么解决方法也很简单,在
logging配置文件中将
disable_existing_loggers设为
False(或者
false)即可。
Reference
- Logging HOWTO — Python 3.8.1rc1 documentation
- logging - Can’t get access log to work for gunicorn - Stack Overflow
END
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- 找到的OpenLayers跨域访问WFS服务出现No 'Access-Control-Allow-Origin' header is present...错误的一种解决办法
- Keil 不能查找字符的一种可能解决办法
- ArcGIS 10.1 for Server 数据注册失败的一种可能原因及解决办法
- 数据库莫名变慢的一种可能原因和解决办法.
- [Java] java.lang.OutOfMemoryError: PermGen space的一种可能解决办法
- 关于PHP+Mysql1045 Access denied for user错误解决办法――你可能想不到的
- windows 8.1 IIS8 发布MVC5项目时,提示“该操作需要IIS集成管线模式”的可能的一种解决办法
- 关于PHP+Mysql1045 Access denied for user错误解决办法——你可能想不到的
- Eclipse:The user operation is waiting for background work to complete的一种可能出错及解决办法
- Access denied for user 'root'@'localhost' (using password: YES)的一种解决办法
- [备忘]SQL Server 2005数据库varchar插入中文出现乱码的一种可能及解决办法
- 解决Apache日志文件ACCESS.LOG日益膨胀的一个办法
- Laravel报class does not exist的一种可能原因及其解决办法
- visual studio编写C#代码时“未能从程序集.....中加载类型”和“找不到方法”的一种可能的解决办法
- 解决Apache日志文件ACCESS.LOG日益膨胀的一个办法
- visual studio编写C#代码时“未能从程序集.....中加载类型”和“找不到方法”的一种可能的解决办法
- IE 打开网页出错/退出、360 打开界面后退出的另外一种可能及解决办法
- 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法
- 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法
- Linux加载驱动"invalid module format"错误的一种可能解决办法