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

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

我是通过命令

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

设置.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.
django uwsgi
1个回答
0
投票

根据您提供的配置,您似乎已经在 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 并检查日志是否正在写入指定文件。

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