Search code examples
djangodockernginxuwsgigoogle-cloud-run

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


I'm using Django, uwsgi and nginx in Cloud Run.

A Cloud Run service can't show django logging and error log. That's why Error Report can't use ,too.

Cloud Run log is like this. It can not see stack trace... I can't know what error happened in the server. enter image description here

This is my settings.

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 Run get /var and /dev directorys logs, right? document

Why this setting can't get django logging result and error log for example 500 error.

I wrote loggin like this.

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

Howeve this log can not see in Cloud Run's log.

Please help me!


Solution

  • I found my mistake. I didn't change server_name in nginx.conf

    I changed this line of the file form

    server_name .example.com;

    to

    server_name MY_DOMAIN;

    Then update Cloud Run. I can see the error log now!

    enter image description here

    Error Report works fine ,too!

    enter image description here

    Is this means that did not connect nginx?