Search code examples
pythonuwsgi

uWSGI startup logs formatted as json


uWSGI has an option for disabling logging from the application it is running. This way, I can control my application logging from Python. However, disabling these still leaves the uWSGI startup messages in the usual format, e.g.

[uWSGI] getting INI configuration from /app/uwsgi.ini
*** Starting uWSGI 2.0.18 (64bit) on [Tue Nov 12 08:36:47 2019] ***
compiled with version: 8.3.0 on 12 November 2019 08:35:14
...

The problem with these is that they are not formatted as json, which means that all log messages are shown as errors in my logging viewer, Google Stackdriver.

uWSGI has an option for encoding/formatting logs, see here, specifically the log-encoder = json { ... option.

The problem is that if I encode the logs as json, using the option above, specifically the following,

log-encoder = json {"unix":${unix}, "msg":"${msg}"}

the startup logs simply disappear.

How can I format startup logs as json?


Solution

  • uWSGI has three sources of log messages:

    1. Application logs (python logs)
    2. uWSGI server logs (information about server startup, errors, warnings)
    3. Request logs

    If I'm getting it right, you have problems with logs under point 2 (uWSGI server logs).

    A configuration like this one works for me:

    logger = default stdio
    log-route = default ^((?!\{).)*$
    log-encoder = json:default {"time":"${micros}", "source":"uwsgi", "message":"${msg}"}
    log-encoder = nl
    

    You can separately format the request logs using the logger-req and log-req-encoder options.

    More on JSON logging in uWSGI and full configuration of all log types can be found here