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.

Similar Reads

PHP strtoupper Function: Convert Strings to Uppercase

Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with…

PHP substr Function: How to Extract and Manipulate Strings

The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

PHP implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

PHP Math: Essential Functions with Examples

PHP math may sound like a pretty simple thing, but I assure you it's a set of tools that will…

PHP htmlspecialchars Function: Prevent XSS in HTML Output

A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

PHP array_pop: Remove Last Element from Array Easily

The array_pop function in PHP. Such a small function, yet so handy when you have to work with the last…

PHP filter_input: How to Validate Input Securely

User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

PHP MySQL WHERE: How to Filter Data in MySQL

Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…

PHP array_push Function: How it Works with Examples

PHP array_push lets you add one or more values to the end of an array. It appeared to make adding…

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.