How To Create Logs Into CodeIgniter 4?

Reading Time: 7 minutes

Managing logs is the very important feature of any application. Logs gives a historical notes or data to track any thing on daily basis. Inside this article we will see a very interesting concept to manage logs in CodeIgniter 4.

Step by step we will discuss how to create logs in CodeIgniter 4 and manage it.

Learn More –

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Log Settings Configuration

Basic level log settings and configurations we do inside CodeIgniter 4 application in –


Open this configuration file (It’s a Logger class), we can find several levels of variables. Let’s see all in this discussion.

public $threshold = 4;

$threshold is member variable of Logger class. The number 4 indicates that “Runtime Errors – Don’t need immediate action, but should be monitored.” This is actually the definition we have taken from the same file.

This number is giving the default monitoring flag to logger. Default logger logs the runtime errors. We can put from any number from 0 to 9 or we can pass more threshold values to log all kind of information.

0 = Disables logging, Error logging TURNED OFF
1 = Emergency Messages - System is unusable
2 = Alert Messages - Action Must Be Taken Immediately
3 = Critical Messages - Application component unavailable, unexpected exception.
4 = Runtime Errors - Don't need immediate action, but should be monitored.
5 = Warnings - Exceptional occurrences that are not errors.
6 = Notices - Normal but significant events.
7 = Info - Interesting events, like user logging in, etc.
8 = Debug - Detailed debug information.
9 = All Messages

To Log logs for ERROR, WARNING, INFO we need to make it as –

public $threshold = [4, 5, 7];

Next, member variable we have inside Logger class –

public $dateFormat = 'Y-m-d H:i:s';

Whenever log file will be created, this is the default format of date which will be attached with the naming convention of log file. We can change, if in case we want more different date format.

Next, we have for

public $handlers = [...]; 

Inside this handlers array, we configure the basic settings of log file. Settings include the file extension, file permissions, save log file path etc.

By default when any logs will be created, it will save inside /writable/logs folder. If we want to save all logs into /public folder.

Need to set,

'path' => 'public'

Inside $handlers array, one more important key we have called handles array which contains all types of log levels that it will handle and manage.

'handles' => [

How to Log Message?

To create and manage logs, we use a helper function called log_message(). This function definition we can find at –


Inside this Common.php, we have this function defined.

function log_message(string $level, string $message, array $context = []) {

This helper function is used to log different different type of log levels. If we go to the definition of this function we should see.

Allowed log levels are:
- emergency
- alert
- critical
- error
- warning
- notice
- info
- debug

Create a Controller

Open project into terminal and run this spark command to create controller file.

$ php spark make:controller Site

It will create a file with the name Site.php inside /app/Controllers .

Let’s create any method inside controller. We are going to make use of log_message() function into it to log any message.


namespace App\Controllers;

use App\Controllers\BaseController;

class Site extends BaseController
    public function logMessage(){
        log_message("error", "This is error message logged by Online Web Tutor");
        log_message("alert", "This is alert message logged by Online Web Tutor");
        log_message("emergency", "This is emergency message logged by Online Web Tutor");
        log_message("critical", "This is critical message logged by Online Web Tutor");

Inside this above method you can see, we are logging log levels error, alert, emergency and critical with any custom message.

If we run application that will hit this logMessage() method of Site controller, it will generate log files.

As we discussed by default it will save into /writable/logs folder but when we set like public. It will save inside /public folder.

Say we have configured public and Next we have to go inside /public and check. Log file will be generated and logs the messages into it.

We should see the log file as – publiclog-2021-02-04.log and messages logged as –

ERROR - 2021-02-04 00:34:54 --> This is error message logged by Online Web Tutor
ALERT - 2021-02-04 00:34:54 --> This is alert message logged by Online Web Tutor
EMERGENCY - 2021-02-04 00:34:54 --> This is emergency message logged by Online Web Tutor
CRITICAL - 2021-02-04 00:34:54 --> This is critical message logged by Online Web Tutor

We hope this article helped you to learn about How to Create Logs Into CodeIgniter 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more