ThinkPHP Blog: MySQLnd Plugins: Writing a MySQL Query Logger in PHP
Note: This article was originally published at PHPDeveloper on 18 August 2010.
A new approach to implementing a query logger and potentially more complex features such as monitoring or read/write-splitting is the MySQLnd Userland Handler Extension (mysqlnd_uh, pecl website). The extension lets you register a PHP class as a proxy for every MySQLnd connection. Every call to a function to MySQLnd (usually indirect through mysqli, mysql, pdo_mysql) is passed to the PHP class, which then calls the original MySQLnd function.
They give a simple example to start - a logging function and how to configure it - followed by a more real-world scenario of logging inside of a project plugin.