CakePHP 4 How To Create Application Logs Tutorial

Share this Article
Reading Time: 5 minutes
384 Views

Inside this article we will see CakePHP 4 how to create application logs. Article will give you the complete idea about creating application logs in CakePHP. There are several types of error levels available.

This tutorial is very simple to understand to and easy to implement if you want to log application logs. According to CakePHP 4 documentation we have these log levels available to use and track them.

  • Emergency
  • Alert
  • Critical
  • Error
  • Warning
  • Notice
  • Info
  • Debug

We can use any of these levels to create application logs. This article with classified information will give you a clear concept about CakePHP 4 logs.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Logs Settings

Open app.php from /config folder. Search for Log.

//...

    /*
     * Configures logging options
     */
    'Log' => [
        'debug' => [
            'className' => FileLog::class,
            'path' => LOGS,
            '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'],
        ],
        // To enable this dedicated query log, you need set your datasource's log flag to true
        'queries' => [
            'className' => FileLog::class,
            'path' => LOGS,
            'file' => 'queries',
            'url' => env('LOG_QUERIES_URL', null),
            'scopes' => ['queriesLog'],
        ],
    ],

//...

We can see we have 3 array keys (debug, error, queries) available inside Log array.

  • debug uses debug.log file to log messages. It logs messages of notice, info, debug level logs.
  • error uses error.log file to log messages. It logs messages of warning, error, critical, alert, emergency level logs.
  • queries this is for dedicated query log.

Create Logs

There is very few simple steps which needs to be followed to create application logs. When we create logs, it will be stored inside /logs folder.

When you open /logs folder, you should see few log files inside it like cli-debug.log, cli-error.log, debug.log, etc.

To create logs in application we have several methods. We will about each.

  • Using write() method
  • Using log level method
  • Using log helper method


Using write() method

To use write method, import Log class.

use Cake\Log\Log;

Next, we can call write() method to log messages.

Syntax

Log::write(<LOG LEVEL>, <MESSAGE>);

Example

Log::write('info', 'Something did not work');

Log::write('emergency', 'Something did not work');

Here, we have logged messages for info and emergency level for now. You can find these logged messaged into debug.log (for info level message) and error.log (for emergency level message).


Using log level method

To call respective methods of log level, we need to import Log class again.

use Cake\Log\Log;

We have all methods available according to level type –

// for debug.log
Log::info("Something did not work");
Log::notice("Something did not work");
Log::debug("Something did not work");

// for error.log
Log::emergency("Something did not work");
Log::error("Something did not work");
Log::critical("Something did not work");
Log::warning("Something did not work");
Log::alert("Something did not work");


Using log helper method

To use this approach we don’t need to import Log class here. Instead we can directly a log() helper method to log application messages.

Syntax

$this->log(<MESSAGE>, <LOG LEVEL>);

Example

$this->log('Something did not work', 'debug');

For more information about CakePHP 4 Logs, click here for official documentation.

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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5