定制 tourze/workerman-psr-logger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/workerman-psr-logger

最新稳定版本:0.0.1

Composer 安装命令:

composer require tourze/workerman-psr-logger

包简介

A PSR Logger integrate with Workerman::logger() function

README 文档

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.

统计信息

  • 总下载量: 249
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-02