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 arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…
PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…
PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…
So, what is a function? Quite simply, a function in PHP is a set of instructions you write once, and…
The increment and decrement are operators that can be used to increase or decrease the variable values which have a…
PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…
You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…
The variable scope in PHP refers to the variables, functions, and classes that can be accessed within different parts of…
There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…
The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…