PHP code example of understand / understand-monolog

1. Go to this page and download the library: Download understand/understand-monolog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

understand / understand-monolog example snippets


use Monolog\Logger;

// input token from Understand.io
$inputToken = 'ab1cd234-1234-45e6-789f-gh1fa1234567';

// choose a handler, either async or sync (see below)
$understandAsyncHandler = new UnderstandMonolog\Handler\UnderstandAsyncHandler($inputToken); // async handler
$understandSyncHandler = new UnderstandMonolog\Handler\UnderstandSyncHandler($inputToken); // sync handler

$monologLogger = new Logger('name');
$monologLogger->pushHandler($understandAsyncHandler); // or $understandSyncHandler

$monologLogger->addError('first error');

$exception = new \DomainException('This is Exception', 123);

$encoder = new \UnderstandMonolog\Encoder\ExceptionEncoder();
$array = $encoder->exceptionToArray($exception);

print_r($array);exit;

//Array
//(
//    [message] => This is Exception
//    [class] => DomainException
//    [code] => 123
//    [file] => /home/vagrant/share/understand-lumen-test/app/Exceptions/Handler.php
//    [line] => 30
//    [stack] => Array
//        (
//            [0] => Array
//                (
//                    [class] => App\Exceptions\Handler
//                    [function] => report
//                    [args] => Array
//                        (
//                            [0] => DomainException
//                        )
//
//                    [type] => method
//                    [file] => /home/vagrant/share/understand-lumen-test/vendor/laravel/lumen-framework/src/Application.php
//                    [line] => 354
// .......... and more


use Monolog\Logger;

// input token from Understand.io
$inputToken = 'ab1cd234-1234-45e6-789f-gh1fa1234567';

// choose a handler
$understandAsyncHandler = new UnderstandMonolog\Handler\UnderstandAsyncHandler($inputToken); // async handler

$monologLogger = new Logger('name');
$monologLogger->pushHandler($understandAsyncHandler);

$exception = new \DomainException('This is Exception', 123);

$encoder = new UnderstandMonolog\Encoder\ExceptionEncoder();
$context = $encoder->exceptionToArray($exception);

$monologLogger->addError($exception->getMessage(), $context);