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

How to Update MySQL Data with PHP?

Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…

PHP $_ENV: Manage Environment Variables

In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…

PHP $_SERVER: Basics and Examples

PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

Learn How to Insert Documents in MongoDB Using PHP

Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

PHP filter_id Function: How to Retrieve PHP Filter IDs

Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…

PHP $_SESSION: Secure Your Web Applications in PHP

It’s very important to remember user data for each session when building web applications. This enables a high level of…

PHP Arrow Functions: Understanding “fn” Syntax

Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…

PHP fclose(): Close Access of File Manipulation

Actually, PHP has a built-in function that doesn’t get the spotlight—fclose(). It ends the file manipulation code. Let’s break down…

PHP array_diff_assoc: How to Compare Arrays with Keys

Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…

OOP Interface PHP: How to Set Rules for Classes

PHP developers relied on class inheritance to share functionality before the OOP interface, but this approach had limits. It solves…

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.