PHP code example of activecollab / logger

1. Go to this page and download the library: Download activecollab/logger 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/ */

    

activecollab / logger example snippets


$factory = new LoggerFactory();
$logger = $factory->create('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir');

$logger->info('Something interesting happened: {what}', [
    'what' => 'really interesting event'
]);

$logger = $factory->create('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir', 'my-awesome-logs.txt', 0777);

$logger = $factory->create('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::GRAYLOG, 'graylog.company.com', 12201);

// Set HTTP request from PSR-7 ServerRequestInterface
$logger->setAppRequest(new \ActiveCollab\Logger\AppRequest\HttpRequest($request));

// Set CLI request from arguments
$logger->setAppRequest(new \ActiveCollab\Logger\AppRequest\CliRequest('session ID', $_SERVER['argv']));

$logger->flushBufferOnShutdown();

$logger = $factory->create('Active Collab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir');

// Additional environment arguments can be set on factory level, and factory will pass them to all loggers that it produces
$factory->setAdditionalEnvArguments([
    'account_id' => 123,
    'extra_argument' => 'with extra value',
]);

// Or you can specify them on logger level
$logger->setAdditionalEnvArguments([
    'account_id' => 123,
    'extra_argument' => 'with extra value',
]);

try {
    // Something risky
} catch (ExceptioN $e) {
    $logger->error('An {exception} happened :(', [
        'exception' => $e,
    ]);
}

$logger->addExceptionSerializer(function ($argument_name, $exception, array &$context) {
    if ($exception instanceof \SpecialError) {
        foreach ($exception->getParams() as $k => $v) {
            $context["{$argument_name}_extra_param_{$k}"] = $v;
        }
    }
});

$factory->addExceptionSerializer(function ($argument_name, $exception, array &$context) {
    if ($exception instanceof \SpecialError) {
        foreach ($exception->getParams() as $k => $v) {
            $context["{$argument_name}_extra_param_{$k}"] = $v;
        }
    }
});

$handler = new ErrorHandler($logger);
$handler->initialize();

$handler->restore();

$handler->setHowToHandleError(E_STRICT, ErrorHandlerInterface::SILENCE);
$handler->setHowToHandleError(E_DEPRECATED, ErrorHandlerInterface::LOG_NOTICE);
$handler->setHowToHandleError(E_NOTICE, ErrorHandlerInterface::LOG_ERROR);
$handler->setHowToHandleError(E_USER_ERROR, ErrorHandlerInterface::THROW_EXCEPTION);

$handler->setReThrowException(false);