tourze / workerman-psr-logger
A PSR Logger integrate with Workerman::logger() function
Installs: 210
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/workerman-psr-logger
Requires
- php: ^8.1
- clue/hexdump: 0.2.*
- psr/log: ^3|^2|^1
- workerman/workerman: ^5.1
- yiisoft/json: ^1.0
This package is auto-updated.
Last update: 2025-10-31 06:07:11 UTC
README
A PSR-3 compatible logger that integrates seamlessly with Workerman's built-in logging system and outputs structured JSON logs.
Features
- Implements PSR-3 LoggerInterface for standard logging
- Integrates with Workerman's built-in logging system
- JSON formatted log output for easy parsing and analysis
- Supports all PSR-3 log levels (emergency, alert, critical, error, warning, notice, info, debug)
- Context support for structured logging
- Extra utility for hexdump and binary data logging
- Automatic timestamp formatting with microsecond precision
- Only logs when Workerman is running (prevents logging in non-daemon mode)
Installation
Requirements:
- PHP >= 8.1
- Workerman >= 5.1
- PSR Log Interface (v1, v2, or v3)
Install via Composer:
composer require tourze/workerman-psr-logger
Quick Start
Basic Usage
<?php use Tourze\Workerman\PsrLogger\WorkermanLogger; $logger = new WorkermanLogger(); // Basic logging $logger->info('Server started'); $logger->error('Connection failed'); $logger->debug('Processing request'); // With context $logger->error('Connection failed', [ 'ip' => '127.0.0.1', 'port' => 8080, 'error_code' => 500 ]);
Log Format
Logs are output as JSON with the following structure:
{
"level": "INFO",
"datetime": "2024-03-24 10:30:45.123456",
"message": "Server started",
"context": {
"ip": "127.0.0.1",
"port": 8080
}
}
Advanced Usage with LogUtil
Use LogUtil for specialized logging scenarios:
use Tourze\Workerman\PsrLogger\LogUtil; // Binary data logging with hexdump LogUtil::debug('Received binary data', $binaryData); // Exception logging with stack trace LogUtil::error('Exception occurred', $exception); // Info logging with optional binary data LogUtil::info('Processing data', $optionalBinaryData); // Warning logging LogUtil::warning('Memory usage high', $memoryDumpData);
API Documentation
WorkermanLogger
Implements Psr\Log\LoggerInterface with the following methods:
emergency(string|\Stringable $message, array $context = []): voidalert(string|\Stringable $message, array $context = []): voidcritical(string|\Stringable $message, array $context = []): voiderror(string|\Stringable $message, array $context = []): voidwarning(string|\Stringable $message, array $context = []): voidnotice(string|\Stringable $message, array $context = []): voidinfo(string|\Stringable $message, array $context = []): voiddebug(string|\Stringable $message, array $context = []): voidlog(mixed $level, string|\Stringable $message, array $context = []): void
LogUtil
Static utility class for specialized logging:
debug(string $message, ?string $binaryData = null): voidinfo(string $message, ?string $binaryData = null): voiderror(string $message, ?\Throwable $e = null): voidwarning(string $message, ?string $binaryData = null): void
Performance Considerations
- Logs are only written when
Worker::isRunning()returns true - JSON encoding is performed for each log entry
- Binary data is hexdumped using the
clue/hexdumplibrary - Context arrays are preserved as-is in the JSON output
Contributing
We welcome contributions! Please follow these guidelines:
- Submit issues for bugs and feature requests
- Follow PSR coding standards
- Write tests for new features
- Ensure all tests pass before submitting PR
- Keep commits focused and atomic
License
The MIT License (MIT). Please see LICENSE for more information.
Changelog
See CHANGELOG.md if available.