Configuring Error Logging in CodeIgniter
Enabling error logging in CodeIgniter isn't just about flipping a switch; it's about configuring how your application handles failures before they reach production users. When you first set up a CodeIgniter project, the framework defaults to displaying errors directly on the screen. While useful for debugging locally, this approach is disastrous in a live environment where end-users should never see raw stack traces or database connection details.
To stop these error pages from appearing and instead route information to a log file, you must modify the config/loggers.php configuration file. Open this file located in your application/config/ directory and locate the $log_threshold variable. By default, this is often set to ERROR, which only logs fatal issues. It is best practice to set this to ALL during development so you can capture warnings, notices, and debug messages. In a production environment, you might stick with ERROR or LOG_ALL_ERRORS depending on your specific logging strategy, but ensuring the threshold is raised prevents silent failures from going unnoticed.
Once the threshold is adjusted, CodeIgniter automatically attempts to create the log file at application/logs/log-$(date-Y-m-d)-$(date-H-i-s).txt. The directory path and filename format are defined by the $log_path and $log_date_format variables within the same config file. You must ensure that the application/logs folder exists and, crucially, that your web server has write permissions for it. On Linux or macOS systems, this usually means setting the folder permissions to 755 or 775 and ensuring the group ownership matches your web server user (often www-data or apache). If the directory does not exist or is read-only, CodeIgniter will throw a fatal error upon initialization before it can even attempt to write to the log.
Pushing an error message into the log file whenever one occurs relies on the framework's internal exception handling and PHP's standard error reporting mechanisms. When a script encounters a runtime error, PHP generates an error level (E_ERROR, E_WARNING, etc.). CodeIgniter intercepts these based on your $log_threshold. If the error level meets or exceeds the threshold, the framework automatically writes the error message, file location, line number, and the source code context to the log file. You do not need to manually instantiate a logger class for every single error; the configuration handles the bulk of this work globally. However, if you need to log custom messages with specific contexts, you can use the Log::write() helper function available in the framework's core utilities.
To push that log data directly to an email address, you need to extend the logging behavior beyond the file system. CodeIgniter supports a "send to email" configuration option defined in the same config file under the $log_send_email setting. When enabled, you specify the recipient address and a subject line prefix. The framework will automatically append the log file content to an email whenever a new log entry is written or at the end of script execution if errors were found. This is useful for critical alerts, though it's important to note that relying solely on email can sometimes lead to message delays or being filtered as spam depending on your server's mail settings.
For many projects, a hybrid approach offers the best balance between immediate visibility and detailed history. You might configure the logger to write to both a file and an email simultaneously by setting up multiple log writers in advanced configurations, though this requires more manual setup using the Log::initialize() method. Additionally, consider the volume of data; logging everything to an email can quickly clog mail servers or trigger anti-spam filters if your application generates frequent warnings.
If you are working on a larger scale and find CodeIgniter's built-in logging insufficient for complex monitoring needs, platforms like https://laravelcompany.com often discuss migration paths or architectural patterns where integrating external logging services might be beneficial. However, for most standard deployments, tweaking the config/loggers.php file provides a robust, zero-cost solution for capturing and alerting on application errors effectively.
