0
0
PhpHow-ToBeginner · 4 min read

How to Log Errors in PHP: Simple Guide with Examples

In PHP, you can log errors using the error_log() function to send error messages to a file or system logger. You can also set a custom error handler with set_error_handler() to control how errors are logged and handled.
📐

Syntax

The basic way to log an error message in PHP is using the error_log() function. It takes a string message and optionally a destination type and destination.

  • error_log(string $message, int $message_type = 0, string $destination = null)
  • $message: The error message to log.
  • $message_type: Where to send the message (0 = system log, 3 = file, etc.).
  • $destination: File path if logging to a file.

You can also create a custom error handler function and register it with set_error_handler() to log errors in your own way.

php
<?php
// Log a simple error message to the system log
error_log("This is an error message.");

// Log an error message to a specific file
error_log("Error happened", 3, "/tmp/my-errors.log");

// Set a custom error handler
function myErrorHandler($errno, $errstr, $errfile, $errline) {
    $message = "Error [$errno]: $errstr in $errfile on line $errline";
    error_log($message, 3, "/tmp/custom-errors.log");
}
set_error_handler("myErrorHandler");
💻

Example

This example shows how to log an error message to a file and how to use a custom error handler to log PHP warnings and notices to a file.

php
<?php
// Log a simple error message to a file
error_log("Simple error logged at " . date('Y-m-d H:i:s') . "\n", 3, __DIR__ . "/error.log");

// Custom error handler function
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    $logMessage = "[" . date('Y-m-d H:i:s') . "] Error [$errno]: $errstr in $errfile on line $errline\n";
    error_log($logMessage, 3, __DIR__ . "/error.log");
    /* Don't execute PHP internal error handler */
    return true;
}

// Register the custom error handler
set_error_handler("customErrorHandler");

// Trigger an error to test logging
echo $undefined_variable; // Notice: undefined variable

// Trigger a warning
include('nonexistentfile.php');

// Output message to confirm script ran
echo "Errors logged to error.log file.";
Output
Errors logged to error.log file.
⚠️

Common Pitfalls

Common mistakes when logging errors in PHP include:

  • Not setting correct permissions on the log file or directory, causing logging to fail silently.
  • Forgetting to set error_reporting level or display_errors in php.ini, so errors are not captured or shown.
  • Using error_log() without specifying the correct message type or destination, leading to logs going to unexpected places.
  • Not returning true in a custom error handler to prevent PHP's default error handler from running.

Example of wrong and right usage:

php
<?php
// Wrong: Logging to a file without write permission
error_log("Test error", 3, "/root/error.log"); // May fail silently

// Right: Use a writable path
error_log("Test error", 3, __DIR__ . "/error.log");

// Wrong: Custom error handler not returning true
function badHandler($errno, $errstr) {
    error_log("Error: $errstr");
    // Missing return true
}
set_error_handler('badHandler');

// Right: Return true to stop PHP internal handler
function goodHandler($errno, $errstr) {
    error_log("Error: $errstr");
    return true;
}
set_error_handler('goodHandler');
📊

Quick Reference

Here is a quick summary of key points for logging errors in PHP:

  • error_log(): Use to send error messages to system log or file.
  • set_error_handler(): Register a custom function to handle and log errors.
  • Permissions: Ensure log files and directories are writable by PHP.
  • error_reporting(): Set which errors to report.
  • display_errors: Turn off in production to avoid showing errors to users.

Key Takeaways

Use error_log() to write error messages to system logs or files easily.
Create custom error handlers with set_error_handler() for flexible error logging.
Always ensure log files have correct write permissions to avoid silent failures.
Return true in custom error handlers to prevent PHP's default error processing.
Configure error_reporting and display_errors settings appropriately for your environment.