uwsgi + flask logging.config无法正常工作,还会中断应用程序 [英] uwsgi + flask logging.config not working and also breaks application

查看:362
本文介绍了uwsgi + flask logging.config无法正常工作,还会中断应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去5个小时一直在寻找答案,却一无所获.

Been looking for an answer for the last 5 hours and found nothing.

我有一个flask(python 2.7)应用程序可以在uwsgi上正常工作,但是我没有日志.

I have a flask (python 2.7) application working fine with uwsgi but I have no logs.

/etc/uwsgi/uwsgi.ini配置:

/etc/uwsgi/uwsgi.ini config:

[uwsgi]
socket = /tmp/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 664
cheaper = 2
processes = 16

/app/uwsgi.ini

/app/uwsgi.ini

[uwsgi]
module = main
callable = app

/app/main.py(无需uwsgi即可正常运行)

/app/main.py (works fine without uwsgi)

app = Flask(__name__)
...
...
...
if __name__ == "__main__":
    with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
        logging.config.dictConfig(json.load(fd))
    app.run()

logging.config.json:

logging.config.json:

...
"formatters": {
  "simple": {
      "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
      "format": "%(asctime)s %(levelname)s %(module)s %(message)s"
    }
...
"handlers": {
"console":{
      "level": "DEBUG",
      "class": "logging.StreamHandler",
      "formatter": "simple",
      "stream" : "ext://sys.stdout"
  },
  "loggers": { },
  "root": {
      "handlers": ["console"],
      "level": "DEBUG"
  }
}

我还尝试将记录器移至main之外(建议有一个帖子),如下所示:

I've also tried to move the logger outside of main (one post suggested it) as follows:

app = Flask(__name__)

with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
    logging.config.dictConfig(json.load(fd))
...
...
...
if __name__ == "__main__":
    app.run()

它破坏了uwsgi:

--- no python application found, check your startup logs for errors ---

与在应用程序声明之前进行日志记录声明相同(但没有错误-该应用程序无法正常工作)

same with logging declaration before app declaration (but without an error - the app just doesn't work)

有什么建议吗?

推荐答案

好,我发现了问题:

"formatters": {
"simple": {
  "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
  "format": "%(asctime)s %(levelname)s %(module)s %(message)s"
}

pythonjsonlogger是我几十年前手动安装并忘记的库.声明logging.config.dictConfig()时未正确加载.

pythonjsonlogger is a library i installed manually decades ago and forgot about. It wasn't loading correctly when declaring logging.config.dictConfig().

此外,对于任何看到此消息的人,uWSGI仅调用app.run(),如果要查看任何日志,则应在声明app之前声明日志记录.例如:

Also, to whomever sees this, uWSGI calls only app.run() and if you want to see any logs you should declare logging before you declare app. eg:

with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
    logging.config.dictConfig(json.load(fd))

app = Flask(__name__)
...
...
...
if __name__ == "__main__":
    app.run()

这篇关于uwsgi + flask logging.config无法正常工作,还会中断应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆