PHP code example of iapotheca / log-processor

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

    

iapotheca / log-processor example snippets


// ...
use App\Logging\RabbitmqLogger;
//...
    'rabbitmq' => [
        'driver' => 'custom',
        'via' => RabbitmqLogger::class, // <-- this is your custom logger
        'exchange-name' => env('LOG_RABBITMQ_EXCHANGE_NAME', 'logs'),
        'host' => env('LOG_RABBITMQ_HOST', 'localhost'),
        'port' => env('LOG_RABBITMQ_PORT', 5672),
        'user' => env('LOG_RABBITMQ_USER', 'guest'),
        'password' => env('LOG_RABBITMQ_PASSWORD', 'password'),
        'log-name' => env('LOG_RABBITMQ_LOG_NAME', 'logstash'),
    ],
//...



namespace App\Logging;

use Iapotheca\LogProcessor\Processor;
use Monolog\Logger;
use PhpAmqpLib\Connection\AMQPSocketConnection;
use PhpAmqpLib\Channel\AMQPChannel;
use Monolog\Handler\AmqpHandler;

class RabbitmqLogger
{
    public function __invoke(array $config)
    {
        $connection = new AMQPSocketConnection(
            $config['host'],
            $config['port'],
            $config['user'],
            $config['password']
        );
        $channel = new AMQPChannel($connection);
        $logger = new Logger($config['log-name']);
        $handler = new AmqpHandler($channel, $config['exchange-name']);
        $handler->pushProcessor(new Processor(config('app.name'), [
            // these are the observed keys within the log message
            'KEY_ONE', 'KEY_TWO'
        ]));
        $logger->pushHandler($handler);
        return $logger;
    }
}

new Processor(
    appName: 'my-app',
    keys: ['SOME_FIELD'],
    preProcessingCallback: null,
    extraSpecialCharacters: ['@']
)
shell
composer