Table of Content
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:
$messageis the text you want to log. (Required)$message_typewhere to send the message. (Optional, default is 0)0= log to server’s error log1= send email2= send to system logger (not supported on all systems)3= write to a custom file
$destinationis 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_headersare 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, "admin@example.com");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.inifor 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 didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…
PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…
Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…
PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…
You can use the array_fill function in PHP when you must fill an array with one value. You may want…
Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…
It’s very important to remember user data for each session when building web applications. This enables a high level of…
The PHP object is an instance from the PHP class or the main data structure that is already been created…
PHP and MySQL have become an inseparable pair for web developers. One handles the logic, while the other stores the…
When you deal with text in PHP, sometimes you need to break it into parts. This is common when users…