CodeForPhilly / paws-data-pipeline

PAWS Data Pipeline Project
MIT License
17 stars 9 forks source link

Format logs on server #532

Closed osabahat closed 8 months ago

osabahat commented 1 year ago

Formats the uwsgi output on the server. Example after launching the app:

$ curl localhost/api/user/test

paws-compose-server  | { "host": "localhost", "address": "172.18.0.4", "method": "GET", "protocol": "HTTP/1.0", "resp_size": "104", "request_body_size": "0", "response_status": "500", "response_time": "0.000251", "uri": "/api/user/test"}

The full list of options for uwsgi logging flags can be found in the docs: https://uwsgi-docs.readthedocs.io/en/latest/LogFormat.html

c-simpson commented 1 year ago

@osabahat Can you update this for 3.11?

osabahat commented 1 year ago

@osabahat Can you update this for 3.11?

@c-simpson I did try 3.11 and 3.12 but the container isn't building the uwsgi python plugin properly, getting a gcc error. I tried installing a couple different versions in the container since the latest one wasn't working but didn't have much luck.

c-simpson commented 1 year ago

Osman,

Looking at your log lines:

{ "host": "localhost:5000",    
"address": "172.18.0.1", "method": "GET", "protocol": "HTTP/1.1",    
"resp_size": "153", "request_body_size": "0", "response_status": "200",    
"response_time": "0.005398", "uri": "/api/user/test"   
}

I don't think that "host" gives much value as it will be added by the log collector. I think they do want a log level for filtering, one of fatal, error, warn, info, debug, trace like "level":"debug" . Maybe log 200s at 'debug' and errors at higher levels by severity?

On the server logging we've got a timestamp like "timestamp": "2023-03-04T20:50:18.774619Z". Do you have a way to add that to enable correlation?

osabahat commented 1 year ago

I removed the hostname field and added a timestamp field formatted the same way as the server logging with the exception of milliseconds (strftime() does not support milliseconds).

I also changed uwsgi's behavior so that by default it will only log 400/500 errors. In order to change this to display other logs, one would need to delete the disable-logging option in the config file. I don't think there's a way for uwsgi to know what the log level of the program is and filtering based on that, as log levels are specific to the Python logging module being used.

Here's a prettified example hitting /api/user/test_fail:

{
"timestamp": "2023-04-10-T02:17:11Z", 
"address": "192.168.80.4", 
"method": "GET", 
"protocol": "HTTP/1.0", 
"resp_size": "135", 
"request_body_size": "0", 
"response_status": "401", 
"response_time": "0.000953", 
"uri": "/api/user/test_fail"
}
osabahat commented 1 year ago

Change "timestamp" format to milliseconds from epoch with %(tmsecs)