Skip to main content

Logger Plugin

The Logger Plugin provides configurable logging capabilities for your application. It allows you to route logs to multiple destinations, including files, stdout, syslog, and external services like Graylog.

Installation

$ composer require phpstreamserver/logger

Example of usage

server.php
use PHPStreamServer\Core\Plugin\Supervisor\WorkerProcess;
use PHPStreamServer\Core\Server;
use PHPStreamServer\Plugin\Logger\Handler\ConsoleHandler;
use PHPStreamServer\Plugin\Logger\LoggerPlugin;

$server = new Server();

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

$server->addWorker(
new WorkerProcess(
name: 'Worker process',
onStart: function (WorkerProcess $worker): void {
$worker->logger->notice('Hello from worker', ['pid' => \posix_getpid()]);
},
),
);

exit($server->run());

Configuration

🔌 LoggerPlugin

OptionTypeDefaultDescription
handlers...Handlernot setA list of logger handlers that define the destinations for the logs.

🔵 ConsoleHandler

Sends log messages to the console (stdout or stderr). Useful for development or debugging purposes where you want to see formatted logs directly in the terminal.

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.
formatterFormatter1Optional. A custom formatter for formatting log messages.

🔵 FileHandler

Writes log messages to a specified file. Supports daily rotation and gzip compression of archived files.

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.
formatterFormatter1Optional. A custom formatter for formatting log messages.

🔵 SyslogHandler

Sends log messages to the system’s syslog service.

OptionTypeDefaultDescription
prefixstringhppssOptional. 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

Sends log messages to a Graylog server using the GELF format. Supports 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.