CakePHP Logging

A CakePHP provides a core configure class for checking the log file, which is stored in the disk to get the status of the data.

Logging is an essential tool for getting the status of the CakePHP application over a specified time. By use of this class in our application, we can check these points.

  1. What type of words are used in the form for a search?
  2. Which types of errors are shown to the user?
  3. How many times have the specific queries been executed in the application?

The connection of logging data in CakePHP is secure. It provides a log() function for defining the error or debugs method in logs class to generate the status of the application.

Logging Configuration

The logs class is defined in the config/app.php file. You can also define the many loggers in the app.php file according to the needs in the cakephp application. Furthermore, you can also define the path of logs file to store the status of the file. Logging can be configured by defining the packagelike use Cake\Log\Log in app.php file.

 use Cake\Log\Log; 
 'Log' => [
  'debug' => [
  'className' => FileLog::class,
  'path' => LOGS, // you can also set 
  'file' => 'debug',
  'url' => env ('LOG_DEBUG_URL', null),
  'scopes' => false,
  'levels' => ['notice', 'info', 'debug'],
  ],
  'error' => [
  'className' => FileLog::class,
  'path' => LOGS,
  'file' => 'error',
  'url' => env ('LOG_ERROR_URL', null),
  'scopes' => false,
  'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  ], 

In the above configuration, we have defined two loggers name as debug and error. Each of these is used to configure the different level of message in the web application. These loggers also create a separate file to store the logs message in the logs folder. So, we can see the message of each files separately without any serious errors.

Here we have listed few loggers’ levels that are supported by the CakePHP –

  • Emergency: It used when the system is unusable.
  • Alert: It represents that action performs immediately.
  • Critical: It shows critical condition.
  • Error: It shows error condition in logs data.
  • Warning: It shows a warning message on the application page.
  • Info: It shows informational messages to the user.
  • Debug: It shows you debug messages.

You can set these loggers’ level by defining the name with the log messages. Or, you can define these methods like Cake\Log\Log :: error () to clearly shown the logging level.

Write messages in a log file

Here we have defined two methods for passing the message in the logs file.

The first method contains the static representation of the data in write () method like

Log:: write (‘debug’ , ‘Something went wrong’ );

And the second method contains the log() function that’s available on any class for showing the message by use of LogTrait. Furthermore, it can be called directly by defining the log() function in the controller for passing the message in logs folder.

$this ->log ( “ something went wrong! “ ,‘debug’);

Example of Write logger:

Now we will create a LogsController.php file at src/Controller/LogsController.php and write the given below program in the controller file to pass the message in logs folder.

This is the LogsController.php file

>?php
  namespace App\Controller;
  use App\Controller\AppController;
  use Cake\Log\Log; // logs package
  use App\Controller\Configure;
  class LogsController extends AppController{
  public function index(){
  /*Here we have used static method for pass the message in debug file inside the logs //folder of debugging. */
  /* It takes two arguments first is loggers level, and the second is the messages which //you want to show in your logs file. */
  Log:: write ( ‘emergency ‘, "Something went wrong."); // 
  Log:: write('debug', "Are you facing any issue?.")
  // either this way for showing the messages in a log files
  /*And this is the second way to write data in log file.*/
  $this->log ( "Some Technical issues", 'error' );
  $this->log ( "Something went wrong.", 'debug' );
  }
  }
 ?> 

Now we have to create an index.ctp file at src/template/Logs folder for showing the message on the application.

This is an index.ctp file inside the Logs Folder of Template.

 Something is written in a log file.>br> Either in Check log file logs\debug.log
 >br> Either in Check log file logs\emergency.log
 >br> Either in Check log file logs\error.log 

When you run this program in localhost like localhost/my_app_name/logs, then it will show the given below image.

 Output:

CakePHP Logging

You can also check the logs file manually to go to C:\wamp64\www\my_app_name\logs

and there will be two files debug and the error file which contain the details of the records in the application.

This is a file of error.txt inside the logs folder.

CakePHP Logging 1

And this is a file of Debug.txt inside the logs folder.

CakePHP Logging 2