To log errors in CodeIgniter, set the log threshold so that error messages are written to log files, then read the files in the log folder. In CodeIgniter 3 you change $config['log_threshold'] in application/config/config.php. In CodeIgniter 4 you set the threshold property in app/Config/Logger.php and read logs from writable/logs.
Logging errors on production gives you a record of problems your visitors hit, so you can fix them without waiting for a bug report.
Open application/config/config.php and find the log settings. The default threshold is 0, which means nothing is logged:
An empty log_path means files go to application/logs/. Make sure this folder exists and is writable by the web server user, otherwise nothing will be written.
| Value | What is logged |
|---|---|
| 0 | Nothing, logging disabled |
| 1 | Error messages, including PHP errors |
| 2 | Debug messages and errors |
| 3 | Informational messages, debug messages and errors |
| 4 | All messages |
You can also pass an array, for example $config['log_threshold'] = array(1, 2);, to log only selected levels.
For production, use 1. Higher levels write much more data, fill the disk quickly and can expose sensitive details in the files.
CodeIgniter 4 is the maintained line, and CodeIgniter 3 no longer receives regular updates, so new projects should use version 4. Logging is configured in app/Config/Logger.php:
The default value depends on the environment. Setting CI_ENVIRONMENT = production in the .env file gives a lower default threshold than development. You can use an integer or an array of levels. Levels follow the PSR-3 style names such as emergency, alert, critical, error, warning, notice, info and debug.
To restrict production logging to serious problems, set a smaller threshold in app/Config/Logger.php or override it in .env:
Check the framework documentation for the exact numeric mapping of your version, because it differs from CodeIgniter 3.
Use the log_message() helper from controllers, models and libraries. It works in both versions:
CodeIgniter 4 also supports placeholders so that values are inserted safely:
Do not log passwords, tokens, card numbers or other personal data.
application/logs/log-YYYY-MM-DD.phpwritable/logs/log-YYYY-MM-DD.logA new file is created for each day. A typical entry looks like this:
On a server you can follow a log live with:
An entry is written whenever the framework or your code hits something at or above the chosen threshold. Common examples are failed database queries, missing views or files, uncaught exceptions and your own log_message() calls. Read the files regularly, fix the recurring errors and remove old files so they do not grow without limit.
The threshold may still be 0, the folder may not be writable, or no error has occurred yet. Check permissions and trigger a test with log_message('error', 'test').
No. Logging all levels fills the disk quickly and may store sensitive data. Log errors, and add more detail temporarily when investigating a problem.
Yes. In CodeIgniter 3 set $config['log_path']. In CodeIgniter 4 set the path option of the file handler in app/Config/Logger.php.
They should not be reachable from the web. In CodeIgniter 4 the writable folder is outside the public directory by design.
Enable logging by raising the threshold above 0, then use log_message() for your own entries. CodeIgniter 3 writes to application/logs and CodeIgniter 4 to writable/logs. Keep production logging to errors, protect the files and review them regularly.