Logger plugin for PHPStreamServer

v0.3.0 2024-12-06 17:26 UTC

This package is auto-updated.

Last update: 2024-12-06 17:57:01 UTC


README

Logger Plugin for PHPStreamServer

PHP >=8.2 Version Downloads

The Logger Plugin for PHPStreamServer extends the core functionality by providing a configurable PSR-compatible logger. It allows to capture and route logs from different channels and severities to various destinations, such as files, stdout, Graylog servers (via the GELF protocol), and syslog.

Features

  • Route logs by channel and severity to different destinations.
  • Rotate file logs.
  • Compress file logs.
  • Customizable format: Supports JSON, human-readable and custom formats.
  • Customizable handlers: Implement your own custom log handlers.

Install

$ composer require phpstreamserver/core phpstreamserver/logger

Configure

Here is an example of a simple server configuration with logger.

// server.php

use PHPStreamServer\Core\Plugin\Supervisor\WorkerProcess;
use PHPStreamServer\Core\Server;
use PHPStreamServer\Plugin\Logger\Handler\ConsoleHandler;
use PHPStreamServer\Plugin\Logger\Handler\FileHandler;
use PHPStreamServer\Plugin\Logger\LoggerInterface;
use PHPStreamServer\Plugin\Logger\LoggerPlugin;

$server = new Server();

$server->addPlugin(
    new LoggerPlugin(
        new ConsoleHandler(),
        new FileHandler(
            filename: __DIR__ . '/log.log',
            rotate: true,
        ),
    ),
);

$server->addWorker(
    new WorkerProcess(
        name: 'Supervised Program',
        count: 1,
        onStart: function (WorkerProcess $worker): void {
            $logger = $worker->container->getService(LoggerInterface::class);
            $logger->info('test message');
        },
    ),
);

exit($server->run());

Run

$ php server.php start