Skip to main content

Logger Plugin

Flexible logging system supporting multiple outputs, including files, stdout, syslog, and Graylog.

Installation​

$ composer require phpstreamserver/logger

HTTP and HTTPS transport for GelfHandler additionally requires the AMPHP HTTP client:

$ composer require amphp/http-client

Usage Example​

server.php
<?php

require __DIR__ . '/vendor/autoload.php';

use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\SupervisedWorker;
use PHPStreamServer\Plugin\Logger\Handler\ConsoleHandler;
use PHPStreamServer\Plugin\Logger\LoggerPlugin;
use Psr\Log\LoggerInterface;

$server = new Server();

$server->addPlugin(
new LoggerPlugin(
new ConsoleHandler(),
),
);

$server->addWorker(
new SupervisedWorker(
name: 'Worker',
onStart: function (SupervisedWorker $worker): void {
$logger = $worker->container->getService(LoggerInterface::class);
$logger->info('Hello from worker', ['pid' => \posix_getpid()]);

// You can also use the logger like this:
// $worker->logger->info('Hello from worker', ['pid' => \posix_getpid()]);
},
),
);

exit($server->run());

Plugin Configuration​

🧩 LoggerPlugin​

Plugin class: LoggerPlugin

ParameterTypeDefaultDescription
handlersHandler ...zero argumentsOptional. Handlers that define where log messages are sent.

Handler Configuration​

The Logger plugin provides various handlers for routing logs to different destinations.

πŸ“ ConsoleHandler​

Handler class: ConsoleHandler
Sends log messages directly to the console (stdout or stderr).

ParameterTypeDefaultDescription
outputint2Optional. Specifies the output stream (1 for stdout, 2 for stderr).
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsarray<string>[]Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels.
formatterFormatterConsoleFormatterOptional. A custom formatter for formatting log messages.

πŸ“ FileHandler​

Handler class: FileHandler
Sends log messages to a specified file, supporting daily rotation and gzip compression of archived logs.

ParameterTypeDefaultDescription
filenamestringrequiredPath to the file where logs will be saved.
rotateboolfalseOptional. Enables daily log rotation.
compressboolfalseOptional. Gzip-compresses rotated logs. Requires ext-zlib.
maxFilesint0Optional. Maximum number of archived log files to retain. Unlimited by default.
permissionint0644Optional. File permissions for the created log file.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[][]Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels.
formatterFormatterStringFormatterOptional. Formatter used for file output.

πŸ“ SyslogHandler​

Handler class: SyslogHandler
Sends log messages to the system’s syslog service.

ParameterTypeDefaultDescription
prefixstringphpssOptional. The string prefix is added to each message.
flagsint0Optional. Bitmask. See php.net for more details.
facilityint8Optional. Specifies the syslog facility. See php.net for more details.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[][]Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels.

πŸ“ GelfHandler​

Handler class: GelfHandler
Sends GELF messages over UDP, TCP, HTTP, or HTTPS.

ParameterTypeDefaultDescription
addressstringrequiredURI beginning with udp://, tcp://, http://, or https://. UDP and TCP require an explicit port; HTTP and HTTPS default to ports 80 and 443.
hostNamestring|nullnullOptional. Hostname included in GELF messages. Defaults to the current hostname.
includeStacktracesboolfalseOptional. Includes stack traces for exceptions found in log context.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[][]Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels.