PHP code example of ecourty / logger-bundle

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

    

ecourty / logger-bundle example snippets


return [
    // ...
    Ecourty\LoggerBundle\LoggerBundle::class => ['all' => true],
];

use Ecourty\LoggerBundle\Service\Logger;

final class PlaceOrderHandler
{
    public function __construct(
        private readonly Logger $logger,
    ) {
    }

    public function handle(Order $order): void
    {
        $this->logger->info(OrderLogContext::Created, 'Order placed', [
            'orderId' => $order->getId(),
        ]);
    }
}

namespace App\Order\Enum;

enum OrderLogContext: string
{
    case Created = 'OrderCreated';
    case PaymentFailed = 'OrderPaymentFailed';
}

namespace App\Security\Logger;

use App\Security\User;
use Ecourty\LoggerBundle\Contract\LogParameterProcessorInterface;

final class UserLogParameterProcessor implements LogParameterProcessorInterface
{
    public function supports(mixed $value): bool
    {
        return $value instanceof User;
    }

    public function process(mixed $value): mixed
    {
        return [
            'id' => $value->getId(),
            'email' => $value->getEmail(),
        ];
    }
}

$this->logger->warning(SecurityLogContext::LoginFailed, 'Login failed', [
    'user' => $user, // serialized by UserLogParameterProcessor
]);

$this->logger->error(OrderLogContext::PaymentFailed, 'Payment declined');
// → "PMS - OrderPaymentFailed - Payment declined"