Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log SQL queries in production?

I'm having a dilemma on whether or not to log SQL queries in production as well.

I don't know how slow writing files is in PHP. Probably some benchmarks could give some answers, but I wanted to see what you guys think before.

What does or does not make the process slow? Or what things could it depend on?

like image 745
treznik Avatar asked Jun 04 '10 17:06

treznik


2 Answers

Most databases have built-in options for logging queries and slow queries, so you shouldn't need log through PHP. You should not log all queries in production unless you are having problems and it's part of a troubleshooting process. You can and should log slow queries so you can see what may be slowing down your production site.

If you framework supports it, you can log queries only if the page took a certain amount of time to generate (this is what I do). Then you are logging conditionally and may discover an excessive number of queries being run.

like image 58
Brent Baisley Avatar answered Oct 05 '22 22:10

Brent Baisley


You have a couple options:

  • have your database log the queries
  • Create a logger class with a static method that uses a cached file handle to write to.. this is pretty fast. Also, you can set this class up to look at a log variable in config to just ignore the incoming sql query or log it to file. Assuming you are using a database API, you can just extend the query function to include this extra line of code for (optional) logging
like image 29
Zak Avatar answered Oct 05 '22 23:10

Zak