uWSGI has a --logformat option for building custom request loglines. The syntax is simple:
[uwsgi]
logformat = i am a logline reporting "%(method) %(uri) %(proto)" returning with status %(status)
All of the variables marked with %() are substituted using specific rules. Three kinds of logvars are defined (“offsetof”, functions, and user-defined).
These are taken blindly from the internal wsgi_request structure of the current request.
These are simple functions called for generating the logvar value:
You can define logvars within your request handler. These variables live only per-request.
import uwsgi
def application(env, start_response):
uwsgi.set_logvar('foo', 'bar')
# returns 'bar'
print uwsgi.get_logvar('foo')
uwsgi.set_logvar('worker_id', str(uwsgi.worker_id()))
...
With the following log format you will be able to access code-defined logvars:
uwsgi --logformat 'worker id = %(worker_id) for request "%(method) %(uri) %(proto)" test = %(foo)'
To generate Apache-compatible logs:
[uwsgi]
...
log-format = %(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)"
...
(Updated to 1.9.21)
You can register new “logchunk” (the function to call for each logformat symbol) with
struct uwsgi_logchunk *uwsgi_register_logchunk(char *name, ssize_t (*func)(struct wsgi_request *, char **), int need_free);
static ssize_t uwsgi_lf_foobar(struct wsgi_request *wsgi_req, char **buf) {
*buf = uwsgi_num2str(wsgi_req->status);
return strlen(*buf);
}
static void register_logchunks() {
uwsgi_register_logchunk("foobar", uwsgi_lf_foobar, 1);
}
struct uwsgi_plugin foobar_plugin = {
.name = "foobar",
.on_load = register_logchunks,
};
Now if you load the foobar plugin, you will be able to use the %(foobar) request logging variable (that would report the request status).