← Back to PHP Course | Chapter 13: Error Handling | Lesson 4 of 5

PHP Error Logging

Error logging is like keeping a diary of everything that goes wrong, written where only you can see it. Visitors see a friendly message while the real details are saved for you.
Syntax
php
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
<?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";
?>

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
<?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");
?>

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
<?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;
?>

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
<?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.";
}
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Logging sensitive data such as passwords or card numbers into a log file.
  2. Writing to a log file that the web server user cannot write to, so nothing is logged.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.