Skip to content

error_log()

Logging PHP Errors with the Error Log Function

Tracking errors in PHP code is essential for debugging and maintenance. The built-in error_log() function provides a straightforward way to log errors, warnings, and custom messages to a file, email, or system logger.

This guide covers how to use the error_log() function effectively, explains its parameters, and outlines its benefits for application stability and security.

What is the error_log() function?

The error_log() function is a built-in PHP function that allows you to log errors, warnings, and other messages to a specified file, email address, or system logger. It is highly configurable, allowing you to specify the message type and destination.

Using error_log() is essential for debugging and troubleshooting, as it allows developers to track and fix errors efficiently. By logging errors, developers gain insights into the root cause of problems, leading to quicker resolution times.

How to Use the error_log() function

Using the error_log() function is straightforward. The function signature is error_log(string $message, int $message_type = 0, ?string $destination = null, ?string $extra_headers = null): bool.

Here's an example:

php
<?php

$error_message = "Error: Unable to connect to the database";
error_log($error_message, 3, "/var/log/php-errors.log");

In this example, we log an error message to the file /var/log/php-errors.log. The 3 in the second parameter specifies the MESSAGE_LOG type, which directs the message to the file path provided in the third parameter. By default, messages are appended to the log file.

The $message_type parameter accepts several values:

  • 0 (default): Sends the message to PHP's system logger.
  • 1: Sends the message to the email address specified in $destination.
  • 2: Sends the message to the IIS event log (Windows only).
  • 3: Appends the message to the file specified in $destination.

When $message_type is 1, the $extra_headers parameter allows you to specify additional email headers, such as From:, Cc:, or Bcc:.

Note: The error_log PHP.ini directive can override the function's default behavior. If configured, it defines the default destination for error_log() calls that do not explicitly specify one.

Benefits of Using the error_log() function

Using the error_log() function provides several benefits for developers and application owners. Here are a few of the most significant benefits:

Improved Debugging and Troubleshooting

By using error_log() to log errors, developers can get valuable insights into the root cause of the problem. This can lead to quicker resolution times, resulting in a better user experience and improved application performance.

Enhanced Security

Logging errors can also help improve application security by identifying and fixing potential vulnerabilities before they can be exploited. For example, logging errors can help developers identify SQL injection attempts, unauthorized access attempts, and other security threats.

Better User Experience

By logging errors, developers can ensure that any issues with the application are fixed promptly, leading to a better user experience. This can lead to increased user satisfaction and retention rates.

Conclusion

In conclusion, the error_log() function is an essential tool for any PHP developer looking to improve their application's performance, security, and overall user experience. By logging errors and warnings, developers can quickly identify and resolve issues, leading to a better application for users.

Practice

What functions does PHP have to handle errors and exceptions?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.