1. Go to this page and download the library: Download meritum/structured-logging 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/ */
meritum / structured-logging example snippets
use Meritum\StructuredLogging\StructuredLoggingModule;
$kernel->addModule(new StructuredLoggingModule());
use Meritum\StructuredLogging\Exception\DomainException;
use Meritum\StructuredLogging\Severity;
final class OrderNotFoundException extends DomainException
{
public function getErrorCode(): string
{
return 'ORDER_' . $this->generateErrorCodeSuffix(1);
}
}
throw new OrderNotFoundException(
message: 'Order not found',
severity: Severity::Warning,
context: ['order_id' => $orderId],
retryable: false,
);
use Throwable;
use Meritum\StructuredLogging\Severity;
use Meritum\StructuredLogging\TranslationHandler;
use Meritum\StructuredLogging\Exception\DomainException;
final class DatabaseExceptionHandler implements TranslationHandler
{
public function matches(Throwable $exception): bool
{
return $exception instanceof \PDOException;
}
public function handle(Throwable $exception): DomainException
{
return new DatabaseException(
message: 'A database error occurred',
severity: Severity::Error,
context: ['pdo_code' => $exception->getCode()],
previous: $exception,
);
}
public function priority(): int
{
return 0;
}
}
// In your module's register() method:
$kernel->define(DatabaseExceptionHandler::class, fn() => new DatabaseExceptionHandler())
->tag('exception.translator.handlers');
use Meritum\StructuredLogging\ExceptionTranslator;
$domain = $translator->translate($e);
use Meritum\StructuredLogging\ExceptionReporter;
$domain = $reporter->report($e);
use Meritum\StructuredLogging\ContextEnricher;
final class AppVersionEnricher implements ContextEnricher
{
public function enrich(array $context): array
{
return $context + ['app_version' => '1.4.2'];
}
}
$kernel->define(AppVersionEnricher::class, fn() => new AppVersionEnricher())
->tag('log.context.enrichers');
use Meritum\StructuredLogging\CorrelationId;
final class CorrelationIdMiddleware implements MiddlewareInterface
{
public function __construct(private readonly CorrelationId $correlationId) {}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$header = $request->getHeaderLine('X-Correlation-ID');
$this->correlationId->set($header);
return $handler->handle($request);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.