CakePHP Logging

Facebooktwitterredditpinterestlinkedinmailby feather

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.

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

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.

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

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.

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


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

Facebooktwitterredditpinterestlinkedinmailby feather