【问题标题】:uWSGI / Flask / Python logs stop after some timeuWSGI / Flask / Python日志在一段时间后停止
【发布时间】:2015-09-02 11:39:18
【问题描述】:

我有一个使用 python 记录器的 uWSGI / Flask 设置。尽管只有一些工人的日志才能到达日志,但一段时间后,甚至那些根本就不再出现了。我的假设是,当 uWSGI 重新启动(克隆)worker 时,日志记录会以某种方式被破坏。有什么想法吗?

app/server.py:

app = Flask(...)
handler = logging.StreamHandler()
app.logger.addHandler(handler)
app.run()

uWSGI:

uwsgi --emperor /etc/uwsgi/apps-enabled/*.ini --die-on-term --uid www-data --gid www-data --logto /var/www/app.com/logs/uwsgi/emperor.log --socket /tmp/uwsgi/emperor.sock --enable-threads --master --single-interpreter --log-reopen --chmod-socket=770

apps-enabled/app-0.ini 和 apps-enabled/app-1.ini 如下所示:

module=server:app
enable-threads=true
single-interpreter=true
master=true
chdir=/var/www/app.com/app
env=APPLICATION_ENVIRONMENT=production
venv=/var/www/app.com/virtualenv

logto=/var/www/app.com/logs/uwsgi/app.com-0.log
log-reopen=true
chmod-socket=770
buffer-size=65535

lazy-apps=true
max-requests=5000
heartbeat=15

for=0 1 2 3 4 5 6 7
socket=/tmp/uwsgi/app.0.%(_).sock
endfor=

processes=8

map-socket=0:1
map-socket=1:2
map-socket=2:3
map-socket=3:4
map-socket=4:5
map=socket=5:6
map=socket=6:7
map=socket=7:8

我也尝试使用SysLogHandler 获得相同的结果。

【问题讨论】:

  • 我遇到了同样的问题。一段时间后,没有任何记录。我正在使用烧瓶/uwsgi/nginx。
  • 嗨,你解决了这个问题了吗?从昨天开始遇到同样的问题,但无处可去!
  • @bool.dev,我还没有找到解决方案。最后恢复到默认的 uwsgi 日志记录。
  • 酷,你能告诉我你的默认uwsgi日志到底是什么意思吗?
  • 我正在使用TimedRotatingFileHandler,它工作得非常好。如果您提供更多详细信息,我可以帮助您吗

标签: python logging flask uwsgi


【解决方案1】:

我不确定这对你的情况是否有帮助,但你可以尝试在 uWSGI 中使用 post forking。

postfork 装饰器只是门票。您可以声明多个 postfork 任务。每个修饰函数都会在每个fork()之后依次执行。

@postfork
def init_logging():
    app.logger.addHandler(handler)

或者您可以在uwsgi.ini 中指定lazy-apps=true

详情:http://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.htmlhttp://uwsgi-docs.readthedocs.io/en/latest/articles/TheArtOfGracefulReloading.html

【讨论】:

    【解决方案2】:

    我可以肯定地告诉你,重启 uwsgi 不应该停止记录。我有 nginx-uwsgi-django 服务器,我一直在重新启动我的 uwsgi 服务器,我的日志永远不会停止。这是我的ini文件的样子:也许你可以根据这个调整你的ini文件,看看它是否有效。

    1 #mysite_uwsgi.ini
    2 [uwsgi]
    3 
    4 # Django-related settings
    5 # the base directory (full path)
    6 chdir           = /home/user/bdapps_stage
    7 # Django's wsgi file
    8 module          = mysite.wsgi:application
    9 # the virtualenv (full path)
    10 home            = /home/user/.conda/envs/mysite_env/
    11 
    12 # process-related settings
    13 # master
    14 master          = true
    15 # maximum number of worker processes
    16 processes       = 3
    17 # maximum number of threads to use
    18 # threads
    19 # the socket (use the full path to be safe
    20 socket          = /home/user/mysite/mysite.sock
    21 # ... with appropriate permissions - may be needed
    22 chmod-socket    = 666
    23 #set the sockets listen queue size
    24 #listen
    25 # clear environment on exit
    26 vacuum          = true
    

    这就是我如何重新启动我的 uwsgi

    kill -SIGHUP [pid id of your uwsgi master]
    

    请注意,您使用 ini 文件为 uwsgi 提供的命令只能用于启动 uwsgi 服务器一次。想重启的时候,建议你记下uwsgi master的pid,使用

     ps -ef | grep uwsgi 
    

    然后运行上面的 kill 命令。

    【讨论】:

      【解决方案3】:

      冒险一段时间后记录停止的另一个可能原因。 uWSGI 可以设置为删除权限(这是个好主意)。但是在此事件之后使用logto2 [0] 设置:

      logto=/var/www/app.com/logs/uwsgi/app.com-0.log
      logto2=/var/www/app.com/logs/uwsgi/app.com-normaluser-0.log
      

      希望对某人有所帮助。

      [0]https://uwsgi-docs.readthedocs.io/en/latest/Options.html#logto2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多