How to configure uwsgi to encode logging as json except app output

2024/10/14 5:16:55

I'm running uwsgi around a Python Flask webapp with these options (among others) to get JSON-encoded log records on stdout:

fmt=$'{"timestamp": "${strftime:%FT%TZ}", "level": "DEBUG", "name": "uwsgi", "message": "${msg}"}\n'uwsgi --env="TZ=UTC" --log-encoder="json ${fmt}" --logformat="%(status) [%(msecs)ms] %(method) %(uri)"

This nicely encodes the stdout from uwsgi but unfortunately also encodes the logging from my application, which is already in JSON format so I get things like:

{"timestamp": "2017-10-02T22:48:11Z", "level": "DEBUG", "name": "uwsgi", "message": "spawned uWSGI http 1 (pid: 75298)"}
{"timestamp": "2017-10-02T22:48:15Z", "level": "DEBUG", "name": "uwsgi", "message": "{\"timestamp\": \"2017-10-02T22:48:15.200Z\", \"message\": \"Descriptor requested\", \"request\": \"c6b08680-a7c3-11e7-9495-186590cba8eb\", \"name\": \"myapp.main\", \"level\": \"INFO\"}"}
{"timestamp": "2017-10-02T22:48:15Z", "level": "DEBUG", "name": "uwsgi", "message": "200 [11ms] GET /descriptor.json"}

The middle record has been wrapped in the same JSON encoding as other output from uwsgi.

How do I avoid the output from my Flask application being encoded but keep encoding of other output from uwsgi itself?

I've tried various combinations of --log-encoder and --log-req-encoder but the latter seems to encode nothing. The docs are not very clear on the distinction between the two options.

Answer

In the end I had to remove --log-encoder and pass stdout (and stderr) through a process that wrapped output in JSON unless already encoded as JSON.

function log_json() { python -u -c "import sysimport jsonimport datetimelog = dict(name='uwsgi', level='$1')line = sys.stdin.readline()while line:line = line.strip()if line.startswith('{') and line.endswith('}'):print(line)elif line:log['timestamp'] = datetime.datetime.utcnow().isoformat() + 'Z'log['message'] = lineprint(json.dumps(log))line = sys.stdin.readline()
"; }{ uwsgi ... 2>&1 1>&3 3>&- | log_json ERROR; } 3>&1 1>&2 | log_json INFO

If you just want to encode request logging, add --logger-req=stdio option to get --log-req-encoder to encode request logging properly.

https://en.xdnf.cn/q/69450.html

Related Q&A

Testing aiohttp client with unittest.mock.patch

Ive written a simple HTTP client using aiohttp and Im trying to test it by patching aiohttp.ClientSession and aiohttp.ClientResponse. However, it appears as though the unittest.mock.patch decorator is …

GridsearchCV: cant pickle function error when trying to pass lambda in parameter

I have looked quite extensively on stackoverflow and elsewhere and I cant seem to find an answer to the problem below. I am trying to modify a parameter of a function that is itself a parameter inside …

How to insert a carriage return in a ReportLab paragraph?

Is there a way to insert a carriage return in a Paragraph in ReportLab? I am trying to concatenate a "\n" to my paragraph string but this isnt working. Title = Paragraph("Title" + …

How to get predictions and calculate accuracy for a given test set in fast ai?

Im trying to load a learner which was exported by learn.export() and I want to run it against a test set. I want my test set have labels so that I can measure its accuracy. This is my code: test_src = …

Splitting the legend in matploblib

Is it possible to split up a single big legend into multiple (usually 2) smaller ones.from pylab import *t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) plot(t, s, linewidth=1.0, label="Graph1") g…

Python 3.x - iloc throws error - single positional indexer is out-of-bounds

I am scraping election data from a website and trying to store it in a dataframe import pandas as pd import bs4 import requestscolumns = [Candidate,Party,Criminal Cases,Education,Age,Total Assets,Liabi…

Supposed automatically threaded scipy and numpy functions arent making use of multiple cores

I am running Mac OS X 10.6.8 and am using the Enthought Python Distribution. I want for numpy functions to take advantage of both my cores. I am having a problem similar to that of this post: multithre…

Golang net.Listen binds to port thats already in use

Port 8888 is already bound on my (OS X 10.13.5) system, by a process running inside a docker container:$ netstat -an | grep 8888 tcp6 0 0 ::1.8888 *.* LISTE…

Aiohttp, Asyncio: RuntimeError: Event loop is closed

I have two scripts, scraper.py and db_control.py. In scraper.py I have something like this: ... def scrape(category, field, pages, search, use_proxy, proxy_file):...loop = asyncio.get_event_loop()to_do…

Python for ios interpreter [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Python or Ruby Interpreter on iOS I just discovered this apps pypad and python for ios They have like an interpreter an ed…