Log collection – CakePHP & Phplogger

Error collection with Phplogger is fairly easy, you would need to install Phplogger client from the official page and a working Phplogger token, which can be found on the Product setup page.

In case you don’t have your development environment set up, here is a nice tutorial on the basic PHP project configuration with Docker – https://blog.phplogger.com/2019/07/11/creating-a-php-project-with-docker/

Installation

To install the Phplogger client you would need Composer dependency management. Go to your project folder and run the composer require command in the terminal.

composer require phplogger/phplogger

Next we need to locate the bootstrap file, usually at ./config/bootstrap.php, where we will write our universal log collection configurations. At the very end of the file add the following lines, which would initialize the logger object and set it as default handler for all of the logs. Then replace b444ac06613fc8d63795be9ad0beaf550 with your token from Product setup page.

Log::setConfig('default', function () {
    return new \PhpLogger\Logger(
        'b444ac06613fc8d63795be9ad0beaf550'
    );
});

For the older CakePHP versions (before 3.8) please use config method like so

Log::config('default', function () {
    return new \PhpLogger\Logger(
        'b444ac06613fc8d63795be9ad0beaf550'
    );
});

Optionally you can disable default CakePHP log collection into local files. We highly recommend doing it.

// Optionally stop using the now redundant default loggers
Log::drop('debug');
Log::drop('error');
Log::drop('queries');

Good job! Now everything you log will be available to you at phplogger.com.