PHP Error Logging
In this page:
error_log("message"); // server log
error_log("message", 3, "path/to/file.log"); // custom file
set_error_handler("handler_function");
Introduction to Error Logging
Showing raw errors to visitors leaks implementation details and looks unprofessional; error_log() writes error messages to the server's log file instead, where developers can review them without exposing anything to users.
Example: Introduction to Error Logging
<?php
// Call `error_log("A test error message from the application")`
error_log("A test error message from the application");
// Print "Error written to the server log, not shown to visitors" to the output
echo "Error written to the server log, not shown to visitors";
?>
Login to try C/C++/Java/PHP code in the editor
Logging to Custom Files
Passing 3 as the message type to error_log(), along with a file path, redirects that specific message into your own custom log file rather than PHP's default system log, which helps keep application logs organized.
Example: Logging to Custom Files
<?php
// Call `error_log("Custom log entry" . PHP_EOL, 3, "app_errors.log")`
error_log("Custom log entry" . PHP_EOL, 3, "app_errors.log");
// Print `file_get_contents("app_errors.log")` to the output
echo file_get_contents("app_errors.log");
?>
Login to try C/C++/Java/PHP code in the editor
Logging inside Error Handlers
Combining a custom error handler with error_log() means every PHP-level error gets written to a log automatically, without needing to add logging calls throughout your codebase by hand.
Example: Logging inside Error Handlers
<?php
set_error_handler(function ($errno, $errstr) {
// Call `error_log("Handled error: $errstr")`
error_log("Handled error: $errstr");
// Print "Logged automatically" to the output
echo "Logged automatically";
});
// Print `$undefinedVar` to the output
echo $undefinedVar;
?>
Login to try C/C++/Java/PHP code in the editor
Exception Logging
Wrapping risky code in try-catch and logging the exception's message and stack trace keeps users away from scary technical detail while still giving you everything you need to diagnose the failure later.
Example: Exception Logging
<?php
// Try running this block; jump to `catch` if it throws
try {
// Throw a new `Exception` with message "Payment failed"
throw new Exception("Payment failed");
// Catch Exception $e
} catch (Exception $e) {
// Call `error_log($e->getMessage() . "\n" . $e->getTraceAsString())`
error_log($e->getMessage() . "\n" . $e->getTraceAsString());
// Print "Something went wrong. Please try again." to the output
echo "Something went wrong. Please try again.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Log File Management
Log files that grow unchecked slow down searches and can fill up disk space; periodically checking file size (or rotating logs on a schedule) keeps debugging fast and prevents storage issues.
Example: Log File Management
<?php
// Call `file_put_contents("app_errors.log", "Sample log line\n", FILE_APPEND)`
file_put_contents("app_errors.log", "Sample log line\n", FILE_APPEND);
// Declare `$size`, set to `filesize("app_errors.log")`
$size = filesize("app_errors.log");
// Print "Log file size: $size bytes" to the output
echo "Log file size: $size bytes";
?>
Login to try C/C++/Java/PHP code in the editor
- Logging sensitive data such as passwords or card numbers into a log file.
- Writing to a log file that the web server user cannot write to, so nothing is logged.
- Forgetting to enable
log_errors, so errors are not written to the log at all.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: