Skip to main content

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

OptionTypeDefaultDescription
handlersHandler[]not setA 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).

OptionTypeDefaultDescription
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.
channelsstring[]not setOptional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default.
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.

OptionTypeDefaultDescription
filenamestringnot setPath to the file where logs will be saved.
rotateboolfalseOptional. Rotate log files.
compressboolfalseOptional. Compresses archived log files to save disk space.
maxFilesint0Optional. The maximum number of log files to keep. Set to 0 to keep all files.
permissionint0644Optional. File permissions for the created log file.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[]not setOptional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default.
formatterFormatterConsoleFormatterOptional. A custom formatter for formatting log messages.

📝 SyslogHandler

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

OptionTypeDefaultDescription
prefixstringphpssOptional. The string prefix is added to each message.
flagsint0Optional. Bitmask. See php.net for more details.
facilityint8Optional. Specify what type of program is logging the message. See php.net for more details.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[]not setOptional. 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.

OptionTypeDefaultDescription
addressstringnot setThe Graylog server address. UDP, TCP, or HTTP endpoint.
hostNamestringnot setOptional. The hostname to include in GELF messages. Current hostname by default.
includeStacktracesboolfalseOptional. Whether to include stack traces in error logs.
levelLogLevelLogLevel::DEBUGOptional. Processes logs with severity equal to or greater than the specified level.
channelsstring[]not setOptional. Processes logs only from the specified channels. Prefixing channel with ! to exclude. All channels by default.