PHP error_log Function: How It Works with Examples

PHP error_log Function

Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to fix it. The PHP error_log Function helps you write error messages to a file or email.

Also, you can send a text to the system log. It gives you a way to track problems and fix them.

In this article, you will cover how the error_log function works in PHP with examples.

Understand the error_log Function in PHP

The error_log() function helps you track problems in your PHP code. It lets you write messages when something unexpected happens.

Here are the use cases:

  • Log custom error messages
  • Save details when a script fails
  • Record user actions that cause issues
  • Send errors to an email for quick alerts
  • Keep logs in production that don’t show errors on screen

Here is the syntax:

error_log($message, $message_type, $destination, $extra_headers);

The parameters:

  • $message is the text you want to log. (Required)
  • $message_type where to send the message. (Optional, default is 0)
    • 0 = log to server’s error log
    • 1 = send email
    • 2 = send to system logger (not supported on all systems)
    • 3 = write to a custom file
  • $destination is used with type 1 (email) or 3 (file).
    • This is the recipient address for email.
    • This is the file path for the log file.
  • $extra_headers are the extra email headers (used only with type 1).

Examples of PHP error_log Function

You can use error_log() to write messages directly to a file. This helps you keep a history of errors and debug issues later.

Here is an example:

Pass the message and set the message type to 3. Then give the file path.

error_log("Something went wrong", 3, "/path/to/logfile.log");

This writes the message to the custom file, not the default PHP error log. Make sure the file is writable.

Here is another example of the system logger:

To send the message to the system logger (like syslog on Linux), use message type 0.

error_log("System-level error happened", 0);

The system log location depends on the server setup. On Linux, it goes to /var/log/syslog or /var/log/messages.

You can also use error_log() to send error messages to an email address. This helps you get alerts when something breaks on your server.

Here is an example:

Set the message type to 1 and pass the email address as the third argument.

error_log("Database connection failed", 1, "[email protected]");

Note: Don’t use this too often. If many errors happen at once, it can flood your inbox.

Custom Error Log Path in php.ini

You can change where PHP saves error logs by setting a custom path in the php.ini file.

Open your php.ini file and find this line:

error_log = /path/to/your/php-error.log

Replace the path with the full location where you want PHP to write logs. You have to make sure that the folder exists and has write permissions.

After you save the file, restart your web server for changes to take effect.

Wrapping Up

You learned how the error_log() function works in PHP. You saw how to log messages to a file or send them to the system logger. Also, how to send an email with it. You also learned how to change the log file path with php.ini.

Here’s a quick recap:

  • Use error_log() to track and debug PHP errors.
  • Log messages to a file with type 3.
  • Send alerts by email with type 1.
  • Use the system logger with type 0.
  • Set a custom log path in php.ini for control.

Note: You have to avoid showing errors on-screen in production. Use logs instead.

Click here to see more PHP tutorials.

Previous Article

PHP Singleton Pattern: How to Use with Examples

Next Article

PHP function_exists: Avoid Function Redeclaration

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.