Parse Caddy Json Logs

Hey everyone, this is more to post a small .Net 6 console app I put together to get visitor information for my website and it might just be useful to you too.

The background is that I setup a Hugo static page a while back as I wanted to move away from Wordpress and I know I didn’t want to setup a Medium account. I also wanted some general information about how many visitors I might get in general to my page and what were the most popular urls, but that was about it. I looked around and the easiest setup was using Hugo and Caddy. Love Caddy btw! A few lines of configuration and the page is up and running - https://psyonik.tech/ (No need to visit it unless you’re interested in seeing what it’s like, but there isn’t much on it atm).

The repo for the code can be found here on Gitlab. I added a README with steps on installing git and the .NET 6 sdk on the machine that is supposed to run the code and what the output might look like. Again, this works for me just fine and it cannot search based on date ranges or any other filters just now and if your paths are configured differently, it might also not show you the info you’re interested in, but if you just have a basic Hugo page where content goes into posts then this should help in getting visitor info per log file.

I am also working on a larger project, which will be acting as a full-blown API that reads contents of all the log files passed in, converts these into objects for easier manipulation and then presents them into a front-end. However, that is a bit further down the road from this and it’s still very much a work in progress and this already does what I needed it to do.

As an example use case - since the output is to the console, you could run the application as part of a cron job in which the log file is scanned, the output is piped through to msmtp or mail and you could get a breakdown of which sections of your page gathered the most visitors. Since it reads an entire file, you could also start a new log file once the old one is read, so you could get daily statistics. You could also have it run maybe once a week or once a month and apply the same principle of creating a new log file once this has been read.

The application doesn’t require any special permissions and it doesn’t lock the file while the contents are read, so, as far as I know, Caddy should still be able to write to the log file as this one is reading the contents.

Let me know if you have any questions or feedback around this, I’d be happy to make some other tweaks if someone finds this useful or if you have any questions and want to make the changes yourself. The code is split into the Main class that iterates through the responses received from a data class that reads the contents of the file and returns a sorted dictionary with the keys being the urls visited and the values being a count of the number of times that url shows up in the log file.

Hope it helps! Below is a screenshot of the output including a couple of errors for entries that couldn’t be read correctly as I was still playing around with the different log output options for Caddy:

Console application to read Caddy log files.

PS - sorry if this is the wrong category to post this in, I’d be happy to change it if it’s not relevant here.

I moved this to the Showcase category, which I think is a better fit.

looks cool, I was thinking about doing something like this for my hosted pages, but I’ve been too lazy. Might build off of your project.

Feel free to do so, I might spend some time to change the way the collections are done since it’s a bit slow once the file size increases as it’s not very efficient in finding counts for visited paths.

This topic was automatically closed after 13 days. New replies are no longer allowed.