How to log your heavy database queries in your Laravel project?

Photo by Kevin Ku on Unsplash

How to log your heavy database queries in your Laravel project?

I work in data-intensive Laravel-based applications. So, the biggest problem that occurs as your application grows is that your application gets slowed down. Or, sometimes, the client keeps on saying that the application is running slow, but it is just one query that only the client is running (and God knows why and from where!).

This is a common scenario when your application starts to age, the database keeps getting heavy, and somewhere, that new guy added a database query which was not optimized at all!

Then we need to start the debugging process and check what went wrong! Just to overcome such a problem, (and taking this as an excuse to start my open-source contribution journey), I built a Laravel package to help me track down such heavy queries. I named it as DBLog

Just another Database Logger?

Well... Not actually. I found some great logging tools already available for Laravel. One of my favorites was Laravel Query Logger. But, I needed some more features from that.

Why DBLog?

  • Track the slow queries.: Track slow queries in the log files. (Common feature, I know)

  • Track queries on triggers: Inspired by Laravel Query Logger. Only log the queries based on the triggers.

  • Get the page from which the query ran!: Again, inspired by Laravel Query Logger, I must know the page which is running slow, not just the slow query.

  • Duration Bifurcated Logs: That's a unique feature I wanted. Just logging the slow queries was not enough, sometimes, we need to know the queries which were really slooooow. So, just set up the time brackets and it will log the query based on the time taken. Now, I have the logs of the queries which need immediate attention and then going down the duration list.

  • Datewise Logs:

Client: Hey! I ran this page last week, can you please check that page why was it slow? It's running good today though!

A very common dialog we hear! That feature, just because of that. I don't have to search a single 10 MB log file which contains queries from the past 6 months. I just need to find that date logs and check out the issues. :)

So, I just started my journey to the open source contribution, (finally!!) I really hope that I can be at the service for the community. Please check the package on this Github Link and try it once. I appreciate your reviews, feedback and criticism on it. :)