Very simple remote logging.

I thought up a very simple way to get Nginx to handle remote event logging over http using Nginx’s custom log formats. Very useful for quick and dirty logging anything over http. You can format the logs as CSV and use them elsewhere.

Insert the following within http block (outside the server block):

log_format custom '$time_local, $var1, $var2, $var3, $varN';

Add a location into the server block to handle the custom logging:

    location ~/log/(?<var1>([^?]+))/(?<var2>([^?]+))/(?<var3>([^?]+))/(?<varN>([^?]+)) {
        access_log /var/log/nginx/custom.log custom;
        return 200 'ok';
        add_header Content-Type text/plain;
    }

Restart Nginx.

Hit the url from where ever and insert your values in the path: https://example.com/log/value1/value2/value3/valueN

You should see the csv entry in the custom log file.

Finally, download the log file and import somewhere as a CSV.

I wrote a simple bash script to download the most recent csv and display it in the default spreadsheet software (on a Mac)

#!/bin/bash

# grab the most recent log file
rsync [email protected]:/var/log/nginx/custom.log ./custom.csv

# open it (on a Mac)
open custom.csv

Leave a Reply

Your email address will not be published. Required fields are marked *