【问题标题】:How can I make nginx/gunicorn use structured logging?如何让 nginx/gunicorn 使用结构化日志记录?
【发布时间】:2018-02-09 16:47:22
【问题描述】:

我现在看到

172.19.0.1 - - [09/Feb/2018:07:00:32 +0000] "GET /ping HTTP/1.1" 200 1 "-" "curl/7.47.0"

在我的日志中,但我使用structured logging like this

我什至尝试过

ch = logging.StreamHandler()
ch.setFormatter(pythonjsonlogger.jsonlogger.JsonFormatter())
logging.getLogger("urllib3").addHandler(ch)

但我仍然看到这些消息。我有这样的 nginx/gunicorn (source):

nginx = subprocess.Popen(['nginx', '-c', '/opt/program/nginx.conf'])
gunicorn = subprocess.Popen(['gunicorn',
                             '--timeout', str(model_server_timeout),
                             '-k', 'gevent',
                             '-b', 'unix:/tmp/gunicorn.sock',
                             '-w', str(model_server_workers),
                             'server.wsgi:app'])

我猜这就是日志消息的来源。但我不知道如何在那里获得结构化日志记录。

【问题讨论】:

    标签: python nginx gunicorn


    【解决方案1】:

    Python进程中设置的日志格式与Nginx和Gunicorn中的无关。

    如果要设置 Nginx 日志格式,请参阅

    https://www.nginx.com/resources/admin-guide/logging-and-monitoring/#access_log

    gunicorn,见http://docs.gunicorn.org/en/stable/settings.html#access-log-format

    【讨论】:

    • 从上面的日志信息:gunicorn创建的部分是什么?如何使其成为结构化日志?
    • 我猜上面的日志是gunicorn捕获的,看起来很像默认格式。将--access-logformat 设置为'{"remote_addr": %(h)s...}' 随意
    • 看看我的回答。因为我几乎用 nginx 配置完全解决了它,所以它肯定主要是 nginx。只有“请求”部分可能是 gunicorn
    【解决方案2】:

    找到了。我不得不编辑nginx.conf

    http内:

    log_format structured '{"remote_addr": "$remote_addr", "remote_user": "$remote_user", "request": "$request", "status": $status, "body_bytes_sent": $body_bytes_sent, "http_referer": "$http_referer", "http_user_agent": "$http_user_agent"}';
    

    http > server内:

    access_log /dev/stdout structured;
    

    http://nginx.org/en/docs/http/ngx_http_log_module.html

    现在消息如下所示:

    {
        "remote_addr": "10.32.0.2",
        "remote_user": "-",
        "request": "GET /ping HTTP/1.1",
        "status": 200,
        "body_bytes_sent": 1,
        "http_referer": "-",
        "http_user_agent": "AHC/2.0"
    }
    

    还是待办事项

    如果请求是很好的

    {"method": "GET",
     "endpoint": "/ping",
     "protocol": "HTTP/1.1"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多