如何在 Cloud Run 的日志中显示 django 日志记录和错误日志?
Posted
技术标签:
【中文标题】如何在 Cloud Run 的日志中显示 django 日志记录和错误日志?【英文标题】:How to show django logging and error log in Cloud Run's log? 【发布时间】:2021-11-07 10:38:49 【问题描述】:我在 Cloud Run 中使用 Django
、uwsgi
和 nginx
。
Cloud Run 服务无法显示 django 日志记录和错误日志。这就是为什么Error Report
也不能使用的原因。
Cloud Run 日志是这样的。它看不到堆栈跟踪...我不知道服务器发生了什么错误。
这是我的设置。
Django 设置.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 Run 获取 /var
和 /dev
目录日志,对吗?
document
为什么此设置无法获取 django 日志记录结果和错误日志,例如 500 错误。
我是这样写loggin的。
logger = logging.getLogger(__name__)
logge.info('hello world')
但是在 Cloud Run 的日志中看不到这个日志。
请帮帮我!
【问题讨论】:
@John Hanley 谢谢你的想法。我添加了logging.shutdown()
但是我看不出日志之前的区别。
【参考方案1】:
我发现了我的错误。我没有在 nginx.conf 中更改server_name
我更改了文件格式的这一行
server_name .example.com;
到
server_name MY_DOMAIN;
然后更新 Cloud Run。我现在可以看到错误日志了!
错误报告也可以正常工作!
这是否意味着没有连接nginx?
【讨论】:
以上是关于如何在 Cloud Run 的日志中显示 django 日志记录和错误日志?的主要内容,如果未能解决你的问题,请参考以下文章
使用新的 Google Cloud 日志记录 jar 时,日志未显示在 Google Cloud Platform Stackdriver 中