Skip to content

Integration — Monolog

Buggregator comes with a powerful Monolog server that can receive logs from the popular monolog/monolog package via the \Monolog\Handler\SocketHandler handler. With this feature, you can easily track and analyze the logs generated by your PHP application, making it easier to identify issues and improve its overall performance.

By using Buggregator's Monolog server, you can gain valuable insights into your application's behavior and improve its overall efficiency. So, whether you're a seasoned developer or just starting, the Monolog server in Buggregator is a must-have tool for anyone serious about PHP development.

monolog

Spiral Framework

You can register socket handler for monolog via bootloader.

Bootloader example

php
<?php

declare(strict_types=1);

namespace App\Bootloader;

use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\SocketHandler;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Monolog\Bootloader\MonologBootloader;

class LoggingBootloader extends Bootloader
{
    public function init(MonologBootloader $monolog, EnvironmentInterface $env): void
    {
        $handler = new SocketHandler($env->get('MONOLOG_SOCKET_HOST'), chunkSize: 10);
        $handler->setFormatter(new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES));
        $monolog->addHandler('socket', $handler);
    }
}

Env variables

dotenv
MONOLOG_DEFAULT_CHANNEL=socket
MONOLOG_SOCKET_HOST=127.0.0.1:9913

Laravel

php
// config/logging.php
return [
    // ...
    'channels' => [
        // ...
        'socket' => [
            'driver' => 'monolog',
            'level' => env('LOG_LEVEL', 'debug'),
            'handler' => \Monolog\Handler\SocketHandler::class,
            'formatter' => \Monolog\Formatter\JsonFormatter::class,
            'handler_with' => [
                'connectionString' => env('LOG_SOCKET_URL', '127.0.0.1:9913'),
            ],
        ],
    ],
];

Configuration

dotenv
LOG_CHANNEL=socket
LOG_SOCKET_URL=127.0.0.1:9913

Symfony

Here is an example of how to configure Monolog to send logs to Buggregator in Symfony.

yaml
# config/packages/dev/monolog.yaml
monolog:
  handlers:
    socket:
      level: debug
      type: socket
      formatter: monolog.formatter.json
      connection_string: '%env(MONOLOG_SOCKET_HOST)%'

Configuration

dotenv
MONOLOG_SOCKET_HOST=127.0.0.1:9913

Note: Setting formatter monolog.formatter.json is critical. Otherwise, the Monolog server will not be able to parse the log.

Other PHP frameworks

Install monolog

bash
composer require monolog/monolog
php
<?php

use Monolog\Logger;
use Monolog\Handler\SocketHandler;
use Monolog\Formatter\JsonFormatter;

// create a log channel
$log = new Logger('buggregator');
$handler = new SocketHandler('127.0.0.1:9913');
$handler->setFormatter(new JsonFormatter());
$log->pushHandler($handler);

// Send records to the Buggregator
$log->warning('Foo');
$log->error('Bar');