Logger Plugin
Flexible logging system supporting multiple outputs, including files, stdout, syslog, and graylog.
Installation
$ composer require phpstreamserver/logger
Example of Usage
server.php
use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\WorkerProcess;
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 WorkerProcess(
name: 'Worker process',
onStart: function (WorkerProcess $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
Option | Type | Default | Description |
---|---|---|---|
handlers | Handler[] | not set | A list of logger handlers that specify the destinations for logs. |
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).
Option | 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 | string[] | not set | Optional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default. |
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.
Option | Type | Default | Description |
---|---|---|---|
filename | string | not set | Path to the file where logs will be saved. |
rotate | bool | false | Optional. Rotate log files. |
compress | bool | false | Optional. Compresses archived log files to save disk space. |
maxFiles | int | 0 | Optional. The maximum number of log files to keep. Set to 0 to keep all files. |
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[] | not set | Optional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default. |
formatter | Formatter | ConsoleFormatter | Optional. A custom formatter for formatting log messages. |
📝 SyslogHandler
Handler class: SyslogHandler
Sends log messages to the system’s syslog service.
Option | 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. Specify what type of program is logging the message. 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[] | not set | Optional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default. |
📝 GelfHandler
Handler class: GelfHandler
Sends log messages to a Graylog server in GELF format, supporting UDP, TCP, and HTTP transports.
Option | Type | Default | Description |
---|---|---|---|
address | string | not set | The Graylog server address. UDP, TCP, or HTTP endpoint. |
hostName | string | not set | Optional. The hostname to include in GELF messages. Current hostname by default. |
includeStacktraces | bool | false | Optional. Whether to include stack traces in error logs. |
level | LogLevel | LogLevel::DEBUG | Optional. Processes logs with severity equal to or greater than the specified level. |
channels | string[] | not set | Optional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default. |