How To Create Logs Into CodeIgniter 4 ?

Share this Article
Reading Time: 7 minutes
483 Views

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.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Log Configurations

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

/app/Config/Logger.php

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' => [
     'critical',
     'alert',
     'emergency',
     'debug',
     'error',
     'info',
     'notice',
     'warning',
]

How to Log any Message ?

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

/vendor/codeigniter4/framework/system/Common.php

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/Site.php .

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

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.

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.

Find More on CodeIgniter 4 here

Leave a Comment