https://github.com/tourze/workerman-psr-logger
[READ-ONLY] PSR Logger for Workerman
https://github.com/tourze/workerman-psr-logger
logger psr workerman
Last synced: 3 months ago
JSON representation
[READ-ONLY] PSR Logger for Workerman
- Host: GitHub
- URL: https://github.com/tourze/workerman-psr-logger
- Owner: tourze
- License: mit
- Created: 2025-03-23T17:31:31.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-06-30T13:12:16.000Z (8 months ago)
- Last Synced: 2025-09-23T05:56:23.219Z (5 months ago)
- Topics: logger, psr, workerman
- Language: PHP
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Workerman PSR Logger
[English](README.md) | [中文](README.zh-CN.md)
[](https://packagist.org/packages/tourze/workerman-psr-logger)
[](https://github.com/tourze/workerman-psr-logger/actions)
[](https://scrutinizer-ci.com/g/tourze/workerman-psr-logger)
[](https://packagist.org/packages/tourze/workerman-psr-logger)
[](https://packagist.org/packages/tourze/workerman-psr-logger)
[](https://packagist.org/packages/tourze/workerman-psr-logger)
[](https://codecov.io/gh/tourze/workerman-psr-logger)
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:
```bash
composer require tourze/workerman-psr-logger
```
## Quick Start
### Basic Usage
```php
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:
```json
{
"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:
```php
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 = []): void`
- `alert(string|\Stringable $message, array $context = []): void`
- `critical(string|\Stringable $message, array $context = []): void`
- `error(string|\Stringable $message, array $context = []): void`
- `warning(string|\Stringable $message, array $context = []): void`
- `notice(string|\Stringable $message, array $context = []): void`
- `info(string|\Stringable $message, array $context = []): void`
- `debug(string|\Stringable $message, array $context = []): void`
- `log(mixed $level, string|\Stringable $message, array $context = []): void`
### LogUtil
Static utility class for specialized logging:
- `debug(string $message, ?string $binaryData = null): void`
- `info(string $message, ?string $binaryData = null): void`
- `error(string $message, ?\Throwable $e = null): void`
- `warning(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/hexdump` library
- 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](LICENSE) for more information.
## Changelog
See [CHANGELOG.md](CHANGELOG.md) if available.