from http://php.net/manual/en/function.rename-function.php
bool rename_function (line $ original_name, line $ new_name)
Renames the name orig_name to new_name in the global function table. useful for temporarily overriding built-in functions.
I believe that if you rename the original to original_mysql_query, add a replace function that executes your log, and then call original_mysql_query, etc., that you will achieve your goal, assuming you have a way to enter a rename on each page, it is called MySQL_query. Most large sites have common code that is included at the top of every page that can do this for you.
There is also a built-in php function called override_function (mentioned by ChrisH). It is not fully documented on the php man page, but the user comments below the document give you the information you need to use it if you prefer the rename_function function. The limitation of a single override was discussed if you needed to call the original function from the substitution. Using rename_function instead of overrides eliminates this potential limitation.
source share