Python文件记录。无法在uWsgi下登录

问题描述 投票:0回答:1

我正在使用Python日志记录fileConfig在uWsgi下运行我的Flask应用程序。问题是我无法登录文件或控制台。该应用程序将创建日志文件,但始终为空。当我在不使用uWsgi的调试模式下运行服务时,日志记录工作正常。

我的项目结构:

project/
├── src/
│   └── app.py
├── logging.conf
├── wsgi.py
└── uwsgi.ini

app.py内容:

import logging
from logging.config import fileConfig

from flask import Flask

fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger(__name__)

app = Flask(__name__)


@app.route("/")
def hello():
    logger.info("Got request")
    return "<h1 style='color:blue'>Hello There!</h1>"


if __name__ == '__main__':
    app.run('0.0.0.0')

wsgi.py内容:

from src.app import app

if __name__ == '__main__':
    app.run()

日志记录配置(logging.conf):

[loggers]
keys=root

[handlers]
keys=timedRotatingFileHandler,consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=timedRotatingFileHandler,consoleHandler

[handler_timedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=simpleFormatter
args=("/Users/user/project/logs/project.log", 'MIDNIGHT', 1)

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

uwsgi.ini内容:

[uwsgi]
module = wsgi:app

socket = 0.0.0.0:5000
protocol = http
vacuum = true

die-on-term = true

而且我按如下方式运行所有这些东西:

uwsgi --ini uwsgi.ini 

这里是uWsgi日志:

*** Starting uWSGI 2.0.18 (64bit) on [Thu Apr  9 14:45:08 2020] ***
compiled with version: 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.32.29) on 07 April 2020 07:23:53
os: Darwin-19.3.0 Darwin Kernel Version 19.3.0: Thu Jan  9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64
nodename: User-MacBook-Pro.local
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /Users/user/project
detected binary path: /Users/user/project/venv/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 2784
your memory page size is 4096 bytes
detected max file descriptor number: 10240
lock engine: OSX spinlocks
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3
Python version: 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14)  [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x7fbc6080aa00
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72888 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x7fbc6080aa00 pid: 9587 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 9587, cores: 1)
[pid: 9587|app: 0|req: 1/1] 127.0.0.1 () {42 vars in 1087 bytes} [Thu Apr  9 14:45:26 2020] GET / => generated 40 bytes in 10 msecs (HTTP/1.1 200) 2 headers in 79 bytes (2 switches on core 0)

如何尝试强制我的应用程序按照日志记录设置中的定义进行日志记录?

python flask logging uwsgi
1个回答
0
投票

在配置中一切正常。我只是错过了hello()中的日志记录行。也许该配置将来可能对某人有用。

© www.soinside.com 2019 - 2024. All rights reserved.