如何在 Cloud 运行 的日志中显示 django 日志记录和错误日志?

How to show django logging and error log in Cloud Run's log?

我在 Cloud 运行.

中使用 Djangouwsginginx

云 运行 服务无法显示 Django 日志记录和错误日志。这就是 Error Report 也不能使用的原因。

云运行日志是这样的。它看不到堆栈跟踪...我不知道服务器发生了什么错误。

这是我的设置。

Django settings.py


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'local': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s"
        },

        'verbose': {
            'format': '{message}',
            'style': '{',
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'local',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'WARNING',
    },
    'loggers': {
        'users': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False,
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False,
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    }
}

uwsgi.ini

[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base

# %d is the dir this configuration file is in
socket = %dapp.sock
master = true
processes = 4


[dev]
ini = :base
# socket (uwsgi) is not the same as http, nor http-socket
socket = :8001


[local]
ini = :base
http = :8000
# set the virtual env to use
home=/Users/you/envs/env


[base]
# chdir to the folder of this config file, plus app/website
chdir = %dapp/
# load the module from wsgi.py, it is a python path from 
# the directory above.
module=website.wsgi:application
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666

nginx.conf

# nginx-app.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:/docker/app.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on, default_server indicates that this server block
    # is the block to use if no blocks match the server_name
    listen      8080 default_server;

    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # extend timeout settings
    proxy_connect_timeout       3600;
    proxy_send_timeout          3600;
    proxy_read_timeout          3600;
    send_timeout                3600;

    # no cache
#    sendfile off;
#    etag off;
#    if_modified_since off;

    # max upload size
    client_max_body_size 200M;   # adjust to taste

    # Django media
    location /media  {
        alias /docker/app/website/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /docker/app/website/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
}

supervisor.conf

[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /docker/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx-app]
command = /usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# Graceful stop, see http://nginx.org/en/docs/control.html
stopsignal=QUIT


Cloud 运行 获取 /var/dev 目录日志,对吗? document

为什么此设置无法获取 django 日志记录结果和错误日志,例如 500 错误。

我是这样写loggin的

logger = logging.getLogger(__name__)
logge.info('hello world')

但是这个日志在 Cloud 运行 的日志中看不到。

请帮帮我!

我发现了我的错误。我没有在 nginx.conf

中更改 server_name

我更改了文件格式的这一行

server_name .example.com;

server_name MY_DOMAIN;

然后更新 Cloud 运行。我现在可以看到错误日志了!

错误报告也很好用!

这是没有连接nginx的意思吗?