PHP Function Error_Log
Error logging is an important aspect of PHP development. It allows for error tracking and troubleshooting, allowing developers to find and fix issues before they become more serious. It’s also a great tool for debugging, providing a trail back to where the error originated.
One of the most useful tools for logging errors is the built-in php function error_log. This function enables you to log messages for errors and other exceptions in a customized way that aligns with your specific needs.
The error_log() function takes a message and an integer that signals the error type as its first two parameters. The second parameter is the error message itself, which can be set using a number of pre-defined constants.
These values signal different types of errors: E_ERROR – fatal errors that terminate script execution (e.g., calling nonexistent functions or running out of memory). E_WARNING – runtime warnings that don’t terminate script execution (e.g., a syntax error in an expression).
You can also use the syslog() function to send errors directly to the system logger, which is useful for centralized logging. However, this is less recommended, as you’ll likely want to customize the logging information.
For example, you may want to limit the maximum length of logged errors and also choose a location for error log files, which must be writable by the web server. Lastly, you’ll need to decide whether or not to show the logged messages on screen. The syslog() function has its own parameters for these settings, which are described in the documentation here.