Error collection – 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 following command inthe terminal. This will load the client itself, and few necessary for log data delivery and standardization libraries.

composer require phplogger/phplogger

Next, create an index.php file and paste there the following code. Replace the ‘b444ac06613fc8d63795be9ad0beaf550’ string with your actual token from the Product setup page. This allow you to use Phplogger client for data collection.

<?php
# index.php
include __DIR__ . '/vendor/autoload.php';
$logger = new \Phplogger\Logger('b444ac06613fc8d63795be9ad0beaf550');

Listen for Errors

Now we need to configure the project to listen for Errors in PHP and collect them to Phplogger app. There is a special ErrorHandler class in the Phplogger client, which handles global errors for you. The only thing you need is to register your logger object with that handler. The final version of the basic setup would look like this:

<?php

use PhpLogger\Logger;
use PhpLogger\ErrorHandler;

include __DIR__ . '/vendor/autoload.php';

# create logger
$logger = new Logger('b444ac06613fc8d63795be9ad0beaf550');
# register to handle errors
ErrorHandler::register($logger);

Now all the uncaught exceptions and errors will be sent to Phplogger App and available for analysis.