An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Workerman PSR Logger

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Version](https://img.shields.io/packagist/v/tourze/workerman-psr-logger.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-psr-logger)
[![Build Status](https://github.com/tourze/workerman-psr-logger/actions/workflows/ci.yml/badge.svg)](https://github.com/tourze/workerman-psr-logger/actions)
[![Quality Score](https://img.shields.io/scrutinizer/g/tourze/workerman-psr-logger.svg?style=flat-square)](https://scrutinizer-ci.com/g/tourze/workerman-psr-logger)
[![Total Downloads](https://img.shields.io/packagist/dt/tourze/workerman-psr-logger.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-psr-logger)
[![PHP Version Require](https://img.shields.io/packagist/php-v/tourze/workerman-psr-logger.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-psr-logger)
[![License](https://img.shields.io/packagist/l/tourze/workerman-psr-logger.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-psr-logger)
[![Coverage Status](https://img.shields.io/codecov/c/github/tourze/workerman-psr-logger/main.svg?style=flat-square)](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.