Parse Gunicorn logs with python script
This post will explain parsing the Gunicorn log file using Python script, so lets start:
Lets consider a general example of Gunicorn log file is which is saved as gunicorn.log in the same directory as script:
1
|
[2016-06-13 16:29:15 +0000] [32172] [DEBUG] POST /procurement/web/v2/express/filter_all_view_v2/
|
In above example, first column is showing date and time, and fourth column is showing multiple information like API call, number of workers running currently and connection information.
In order to find out how many times a particulat API get called on a particular date, we will need to extract only that lines that contain information about the API call.
This task can be achieved by following Python script, you will just require to enter a date.
Script does following task:
Script does following task:
- Requires to enter a date of which you need to extract the API call count.
- Opens the log file, gunicorn.log in this case.
- Splits the log file on the basis of new line character and stores it in list.
- Extract the data from the list that corresponds to the given date.
- Now, filters the data so that list only contains the information about API calls.
- Count, how many times a particular API get called.
- At the end, sort the data on the basis of count so that highly shooted API calls should appear first.
1
|
#!/bin/python |
Run this script by issuing following command:
python <script name>
The output will look like:
======================================================
Script to find API's calling count from Gunicorn log
======================================================
Enter the date in format 'yyyy-mm-dd':2016-06-13
2016-06-13
/procurement/web/v2/reserve/filter_all_view_v2/ = 4
/procurement/web/v2/express/filter_all_view_v2/ = 3
/procurement/web/v2/reserve/filter_population/ = 2
/procurement/web/v2/reserve/item/21 = 1
/temp_o/web/v2/user_profile/ = 1
/procurement/web/v2/express/item/2523 = 1
/procurement/web/v2/express/filter_population/ = 1
=====================================================
=====================================================
And you are done.