当Django被uWSGI启动时,它不会将日志写入日志文件

oymdgrw7  于 2023-04-13  发布在  Go
关注(0)|答案(2)|浏览(295)

我通过命令python manage.py runserver启动了Django。Django将日志写入文件my_django_project/log/django.log。到目前为止一切顺利。
我通过命令uwsgi --ini uwsgi.ini启动Django。我希望Django将日志写入文件my_django_project/log/django.log。它创建了该文件,但没有向其中写入任何内容。
我为我的Django项目my_django_project设置了以下设置。
项目布局:

- my_django_project/
  - uwsgi.ini
  - manage.py
  - my_django_project/
    - __init__.py
    - settings.py
    - wsgi.py
  - log/
    - django.log

settings.py:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "[{asctime}] [{name}] [{levelname}] [{filename}:{funcName}:{lineno}] {message}",
            'datefmt': "%Y-%m-%d %H:%M:%S",
            "style": "{",
        },
    },
    "handlers": {
        "console": {
            'level': 'DEBUG',
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "django_file": {
            "level": "DEBUG",
            "class": 'logging.handlers.RotatingFileHandler',
            "formatter": "verbose",
            "filename": "log/django.log",
            'maxBytes': 10 * 1000 * 1000,  # 10 MB
            'backupCount': 3,
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "django_file"],
            "level": "DEBUG",
        },
    },
}

wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_django_project.settings')

application = get_wsgi_application()

uwsgi.ini:

# uwsgi.ini file
[uwsgi]
module=my_django_project.wsgi
master=true
processes=1
http=:8000
vacuum=true
pidfile=/tmp/project-master.pid
logto=./log/uwsgi.log
log-maxsize=10000000
enable-threads=true
thunder-lock=true

uwsgi.log(用XXXX替换了一些私人信息):

*** Starting uWSGI 2.0.21 (64bit) on [Sun Apr  9 12:35:36 2023] ***
compiled with version: Apple LLVM 14.0.0 (clang-1400.0.29.202) on 08 April 2023 04:17:40
os: Darwin-22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103
nodename: XXXXs-MacBook-Air.local
machine: arm64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /Users/XXXX/coding/myapp/my_django_project
writing pidfile to /tmp/project-master.pid
detected binary path: /Users/XXXX/coding/myapp/my_django_project/venv/bin/uwsgi
your processes number limit is 2666
your memory page size is 16384 bytes
detected max file descriptor number: 10240
lock engine: OSX spinlocks
thunder lock: enabled
uWSGI http bound on :8000 fd 7
uwsgi socket 0 bound to TCP address 127.0.0.1:55112 (port auto-assigned) fd 6
Python version: 3.11.2 (main, Mar 24 2023, 00:16:47) [Clang 14.0.0 (clang-1400.0.29.202)]
Python main interpreter initialized at 0x1057e60c8
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145792 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1057e60c8 pid: 95710 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 95710)
spawned uWSGI worker 1 (pid: 95716, cores: 1)
spawned uWSGI http 1 (pid: 95717)
[pid: 95716|app: 0|req: 1/1] 127.0.0.1 () {34 vars in 489 bytes} [Sun Apr  9 04:35:44 2023] GET /api/public/hello-world/ => generated 18 bytes in 60 msecs (HTTP/1.1 200) 8 headers in 239 bytes (1 switches on core 0)
SIGINT/SIGTERM received...killing workers...
gateway "uWSGI http 1" has been buried (pid: 95717)
[deadlock-detector] pid 95716 was holding lock thunder (0x104e38000)
worker 1 buried after 1 seconds
goodbye to uWSGI.
VACUUM: pidfile removed.
vaqhlq81

vaqhlq811#

根据您提供的配置,看起来您已经在Django项目中正确设置了日志记录。但是,uWSGI似乎没有选择Django日志记录配置。
为了在uWSGI中使用Django日志配置,您需要在uwsgi.ini文件中添加以下代码行:

logger = file:log/django.log

这将告诉uWSGI使用Django日志配置中指定路径的文件记录器。
您的uwsgi.ini文件应该如下所示:

# uwsgi.ini file
[uwsgi]
module=my_django_project.wsgi
master=true
processes=1
http=:8000
vacuum=true
pidfile=/tmp/project-master.pid
logger=file:log/django.log
logto=./log/uwsgi.log
log-maxsize=10000000
enable-threads=true
thunder-lock=true

在将这一行添加到uwsgi.ini文件之后,重新启动uWSGI并检查日志是否正在写入指定的文件。

b09cbbtk

b09cbbtk2#

我通过安装django-request-logging包解决了这个问题。

相关问题