CakePHP 4 How To Create Custom Log File in Application

Reading Time: 6 minutes
2,391 Views

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

We create custom log files in application to log our custom error message. When we create logs in CakePHP, it will be stored into /logs folder.

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 creating custom log files for 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.

So, here all the error logs will be logged into debug.log, error.log. All log levels by default uses these files.

If we want our own file to log messages in application, how to configure?

Step #1

Open app.php. Add a new key into Log to log messages.

//...

    /*
     * Configures logging options
     */
    'Log' => [
        'info' => [
            'className' => FileLog::class,
            'path' => LOGS,
            'file' => 'mylogs',
            'url' => env('LOG_DEBUG_URL', null),
            'scopes' => false,
            'levels' => ['notice', 'info', 'debug','warning', 'error', 'critical', 'alert', 'emergency'],
        ],
        '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 a new key as info and it logs messages of these levels –

'levels' => ['notice', 'info', 'debug','warning', 'error', 'critical', 'alert', 'emergency'],

So, whenever we log any messages of notice, debug, warning, etc then logged message will also go into mylogs.log file. This file will be automatically created once we log messages of these levels.

Additionally, the same message will be logged if this level available in other keys of Log like if in debug, error, etc.

Step #2

Create log message to test. We will use write() method for it, So needs to import Log class.

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');

When we log above messages, so as per above settings we will find we have a new file mylogs.log inside /log folder. Into this file we have these message of info and emergency level.

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 Custom Log File in Application 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