Development

PHP error_log() Function

In this tutorial, we will learn how we can send an error message to the defined error handling routines in PHP by using the error_log() function.
Captain Salem 3 min read
PHP error_log() Function

As developers and system administrators, error messages are one of the most critical part of debugging and troubleshooting various applications in our systems.

Errors provides us with a stack of information about what went wrong, where it occurred, when it occurred, and in some cases, why it occurred.

This information enables us to identify and address issues in the code, ensuring that the software functions correctly. In this way, errors are not just problems to be fixed, but rather essential tools that help us improve the quality and reliability of our apps.

It is therefore a good practice to have a few error logging and debugging tools under your belt. One such tool is the error_log() function in PHP.

PHP error_log() Function

As the name suggests, the error_log() function allows us to send log errors or messages to a specified file or system logger.

The function syntax is as shown:

error_log(
    string $message,
    int $message_type = 0,
    ?string $destination = null,
    ?string $additional_headers = null
): bool

The function accepts four main parameters as defined below:

  1. message - this specifies the error message to be logged.
  2. message_type - sets where the error log should go. Accepted values include:
  3. 0 - 0 message types are sent to the PHP's system logger using the OS logging mechanism or file.
  4. 1 - these types of error messages are sent by email to the address defined in the destination parameter.
  5. 3 - type 2 error messages are appended to the file defined in the destination parameter. A newline is automatically added at the end of the message string.
  6. 4- error messages send directly to the SAPI logging handler.
  7. destination - specifies the message destination.
  8. additional_headers - specifies extra headers when the message_type parameter is set to 1.

The function returns a Boolean true on success and false in the case of failure.

Examples

The following shows the various options you can use when working with the error_log() function in PHP.

Example 1 - Send Error Message to Server Log

The example below shows how to send the error message to the server log.

<?php
// Attempt to connect to database
$conn = mysqli_connect($host, $user, $password, $database);

// Check connection status
if (!$conn) {
  $error_message = "Error connecting to database: " . mysqli_connect_error();
  error_log($error_message, 0);
}
?>

In the example above, we use the mysqli_connect() function to connect to the database. If the connection fails, we generate an error message using the mysqli_connect_error() function.

We then send the error message to the server log using the error_log() function and setting the destination as 0. This denotes the destination as the server error log.

Example 2 - Send Notification Via Email

We can also send the error message via email to the administrator as shown in the example below:

<?php
// Attempt to connect to database
$conn = mysqli_connect($host, $user, $password, $database);

// Check connection status
if (!$conn) {
  $error_message = "Error connecting to database: " . mysqli_connect_error();
  error_log($error_message, 0);
  // send a notification to the server administrator
  error_log("Database connection error. Please check the server logs for details.", 1, "admin@example.com");
}
?>

In this case, we use the error_log() function to send the error message to the administrator via email and setting the destination as 1.

Example 3 - Append Error log to File

To append the error message to file using the error_log() function, we can run the code as shown:

<?php
// Attempt to connect to database
$conn = mysqli_connect($host, $user, $password, $database);

// Check connection status
if (!$conn) {
  $error_message = "Error connecting to database: " . mysqli_connect_error();
  error_log($error_message, 3, "/var/www/php-logs.log");
?>

In this case, we send the error message to the /var/log/php-logs.log file.

Conclusion

Congratulations. In this tutorial, you learned how to work with the error_log() function in PHP to send error messages to a specific logger or file in PHP. You learned how you can write the error messages to the server log, send them via email, or append them to a custom log file.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.