Logger Plugin
Flexible logging system supporting multiple outputs, including files, stdout, syslog, Graylog, and VictoriaLogs.
Installationβ
$ composer require phpstreamserver/logger
VictoriaLogsHandler and HTTP and HTTPS transport for GelfHandler additionally require the AMPHP HTTP client:
$ composer require amphp/http-client
Usage Exampleβ
<?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
| Parameter | Type | Default | Description |
|---|---|---|---|
handlers | Handler ... | zero arguments | Optional. 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).
| Parameter | Type | Default | Description |
|---|---|---|---|
output | int | 2 | Optional. Specifies the output stream (1 for stdout, 2 for stderr). |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | array<string> | [] | Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels. |
formatter | Formatter | ConsoleFormatter | Optional. 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
filename | string | required | Path to the file where logs will be saved. |
rotate | bool | false | Optional. Enables daily log rotation. |
compress | bool | false | Optional. Gzip-compresses rotated logs. Requires ext-zlib. |
maxFiles | int | 0 | Optional. Maximum number of archived log files to retain. Unlimited by default. |
permission | int | 0644 | Optional. File permissions for the created log file. |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | string[] | [] | Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels. |
formatter | Formatter | StringFormatter | Optional. Formatter used for file output. |
π SyslogHandlerβ
Handler class: SyslogHandler
Sends log messages to the systemβs syslog service.
| Parameter | Type | Default | Description |
|---|---|---|---|
prefix | string | phpss | Optional. The string prefix is added to each message. |
flags | int | 0 | Optional. Bitmask. See php.net for more details. |
facility | int | 8 | Optional. Specifies the syslog facility. See php.net for more details. |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | string[] | [] | 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
address | string | required | URI beginning with udp://, tcp://, http://, or https://. UDP and TCP require an explicit port; HTTP and HTTPS default to ports 80 and 443. |
hostName | string|null | null | Optional. Hostname included in GELF messages. Defaults to the current hostname. |
includeStacktraces | bool | false | Optional. Includes stack traces for exceptions found in log context. |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | string[] | [] | Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels. |
π VictoriaLogsHandlerβ
Handler class: VictoriaLogsHandler
Sends logs to VictoriaLogs over HTTP or HTTPS using JSON line ingestion.
| Parameter | Type | Default | Description |
|---|---|---|---|
baseUri | string | required | Base HTTP or HTTPS URL of VictoriaLogs, optionally with a path prefix. Query strings and fragments are not allowed. |
appName | string | app | Optional. Application name. |
streamFields | string[] | ['app', 'channel'] | Optional. Log field names whose values identify a log stream. |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | string[] | [] | Optional. List channel names to include, or prefix them with ! to exclude. By default, the handler processes logs from all channels. |