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

0.0.1 2025-04-02 08:17 UTC

This package is auto-updated.

Last update: 2025-10-31 06:07:11 UTC


README

English | 中文

Latest Version Build Status Quality Score Total Downloads PHP Version Require License Coverage Status

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 = []): 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 for more information.

Changelog

See CHANGELOG.md if available.