Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to Do Error Logging in CodeIgniter

PHPCodeIgniter
BestDivision
BestDivision
PublishedMay 1, 2020
UpdatedNov 17, 2026
5 min read

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.

Table of Contents

  • Enable logging in CodeIgniter 3
    • Log threshold values in CodeIgniter 3
  • Enable logging in CodeIgniter 4
  • Write your own log messages
  • Where the log files are saved
  • What gets logged and when
    • Production checklist
  • Frequently Asked Questions
    • Why is my log folder empty?
    • Should I log everything on production?
    • Can I change the log location?
    • Are log files safe to leave on the server?
  • Summary

Enable logging in CodeIgniter 3

Open application/config/config.php and find the log settings. The default threshold is 0, which means nothing is logged:

php

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.

Log threshold values in CodeIgniter 3

ValueWhat is logged
0Nothing, logging disabled
1Error messages, including PHP errors
2Debug messages and errors
3Informational messages, debug messages and errors
4All 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.

Enable logging in CodeIgniter 4

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:

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:

text

Check the framework documentation for the exact numeric mapping of your version, because it differs from CodeIgniter 3.

Write your own log messages

Use the log_message() helper from controllers, models and libraries. It works in both versions:

php

CodeIgniter 4 also supports placeholders so that values are inserted safely:

php

Do not log passwords, tokens, card numbers or other personal data.

Where the log files are saved

  • CodeIgniter 3: application/logs/log-YYYY-MM-DD.php
  • CodeIgniter 4: writable/logs/log-YYYY-MM-DD.log

A new file is created for each day. A typical entry looks like this:

text

On a server you can follow a log live with:

bash

What gets logged and when

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.

Production checklist

  1. Set the environment to production so detailed errors are not shown to visitors.
  2. Keep the threshold low, errors only for CodeIgniter 3.
  3. Make the log folder writable by the web server and not publicly accessible.
  4. Rotate or delete old log files with a scheduled job.
  5. Forward logs to a central tool if you run several servers.

Frequently Asked Questions

Why is my log folder empty?

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').

Should I log everything on production?

No. Logging all levels fills the disk quickly and may store sensitive data. Log errors, and add more detail temporarily when investigating a problem.

Can I change the log location?

Yes. In CodeIgniter 3 set $config['log_path']. In CodeIgniter 4 set the path option of the file handler in app/Config/Logger.php.

Are log files safe to leave on the server?

They should not be reachable from the web. In CodeIgniter 4 the writable folder is outside the public directory by design.

Summary

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.

Share

Similar posts

  • How to Make a Simple REST API in CodeIgniter 4
  • How to Use Grunt in CodeIgniter to Minify CSS and JavaScript
  • CodeIgniter Basics: PHP MVC Framework Introduction
  • How to Do Error Logging in CodeIgniter 3 and 4
  • PayU Payment Gateway Integration in CodeIgniter 4
  • How Do You Create and Access Arrays in PHP?
  • How Do include and require Differ in PHP?
  • What Is Recursion in PHP with Examples?