Debugging and logging are a very important part of developing PHP applications. Especially in the scenario of using databases, rational debugging and recording SQL queries can not only help us quickly locate problems, but also effectively manage debugging information and avoid excessive log files. mysqli::debug is a very useful tool that can help us record detailed database debugging information, but without control, the logs can quickly become huge and even affect the performance of the application.
In this article, we will discuss how to use mysqli::debug to properly control debugging resources and avoid generating excessively large log files.
mysqli::debug is a method in the MySQLi extension that can be used to enable debug mode and record debugging information for all database connections. It allows you to track all queries sent to the MySQL database, get the execution status of the query and related error information, and even capture problems during the database connection process.
$mysqli = new mysqli("localhost", "username", "password", "database");
// Enable debug mode
$mysqli->debug("d:t");
// implement SQL Query
$result = $mysqli->query("SELECT * FROM users");
// Turn off debug mode
$mysqli->debug("t");
Although mysqli::debug provides very detailed debugging information, we cannot always keep debugging mode on in development and production environments, because this will cause log files to swell quickly, which will affect system performance.
We can control whether to enable debugging function according to different environments. In a development environment, we can enable debugging, while in a production environment, we should turn off debugging to avoid generating too large logs.
$mysqli = new mysqli("localhost", "username", "password", "database");
// Determine whether the current environment is a development environment
if ($_SERVER['APP_ENV'] === 'development') {
$mysqli->debug("d:t"); // Turn on debug mode
} else {
$mysqli->debug("t"); // Turn off debug mode
}
To avoid excessive log files, we can combine some strategies to limit the size of the log, such as cutting the log by time or file size, or storing log information into the database.
We can cut the logs based on the time of log generation, so that the size of each log file will not increase infinitely.
$logFile = __DIR__ . '/logs/mysql_debug_' . date('Y-m-d') . '.log';
$mysqli = new mysqli("localhost", "username", "password", "database");
// Enable debugging and write debug information to the specified log file
$mysqli->debug("d:t:" . $logFile);
// implement SQL Query
$result = $mysqli->query("SELECT * FROM users");
In this way, a new log file will be generated every day, and the file name will contain the current date to avoid excessive log file size.
If you want to cut by log file size, you can use the file operation function to detect the file size and cut it. Here is a simple example:
$logFile = __DIR__ . '/logs/mysql_debug.log';
// Check log file size
if (filesize($logFile) > 10 * 1024 * 1024) { // If the file size exceeds 10MB
rename($logFile, __DIR__ . '/logs/mysql_debug_' . time() . '.log'); // Renamed Old Nisshi
}
$mysqli = new mysqli("localhost", "username", "password", "database");
// Enable debugging and write debug information to log files
$mysqli->debug("d:t:" . $logFile);
// implement SQL Query
$result = $mysqli->query("SELECT * FROM users");
During debugging, sometimes the log file may contain URLs or other sensitive information for database queries. In this case, we can replace the domain name part through the code to protect sensitive data from being leaked.
For example, suppose that the log contains a URL like this:
$logMessage = "Request to http://example.com/api/data at " . date('Y-m-d H:i:s');
We can replace the domain name part with m66.net as follows:
$logMessage = "Request to http://m66.net/api/data at " . date('Y-m-d H:i:s');
$logFile = __DIR__ . '/logs/mysql_debug.log';
$mysqli = new mysqli("localhost", "username", "password", "database");
// Enable debug mode
$mysqli->debug("d:t:" . $logFile);
// implement SQL Query
$query = "SELECT * FROM users WHERE email = 'user@example.com'";
$query = str_replace("example.com", "m66.net", $query); // replace URL
$result = $mysqli->query($query);
In this way, we can ensure that the actual sensitive URL domain name is not exposed in the log.
mysqli::debug is a powerful debugging tool, but if not controlled, it may cause excessive log files to affect system performance. By reasonably enabling or disabling debugging functions in the code, cutting logs by time or file size, and processing sensitive information in the log, we can effectively manage debugging resources and log file sizes, thereby ensuring the normal operation of the application.