存储日志的Django Gunicorn

Django Gunicorn where logs are stored

我已经使用一些 tutorial

部署了我的应用程序

我用的是nginx+gunicorn(我用的也是systemd)

现在我的 dev server 一切正常 但是在生产中它失败了 Internal Server Error 当我尝试下载文件时

如何以及在哪里可以找到 gunicorn 日志?(我使用 ubuntu)

此外,这是一段会导致错误的代码,以防万一:

def download_xlsx(request):
    user = request.user
    file_name = request.GET['file_name']
    file_path='main_app/static/xlsx/' + str(user.id) + '/' + file_name
    disposition= 'attachment; filename="' +smart_str(file_name) + '"'
    disposition=disposition.encode('utf-8')
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
            response['Content-Disposition'] = disposition
            return response
    return projects.to_utf8_json_response('not found')

UPD:我试过 运行 sudo journalctl -u gunicorn

但是我得到了一个从 2 个月前开始的巨大文件,所以我无法查看最新的日志来确定它的大小

似乎是这样工作的:

journalctl --unit=gunicorn | tail -n 300

不要忘记在您的 gunicorn 服务文件中设置编码(否则您可能会遇到奇怪的 Unicode 错误,看起来您 运行 遇到了上述函数的这个问题。)

[service]
Environment="LANG=ru_RU.UTF-8"

这可能与您的问题相关: UnicodeEncodeError [Python3/Gunicorn/Nginx/Django]