承接 pixielity/laravel-logging 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pixielity/laravel-logging

Composer 安装命令:

composer require pixielity/laravel-logging

包简介

Logging utilities with formatters, writers, and middleware for structured logging

README 文档

README

Structured logging utilities for Laravel applications with formatters, writers, and middleware for comprehensive request tracking and debugging.

Features

  • JSON Formatter - Single-line JSON logs for production (JSONL format)
  • Hybrid Formatter - Human-readable logs with structured context for development
  • Context-Aware Log Writer - HTTP request logging with automatic context inclusion
  • HTTP Logger Middleware - Automatic HTTP request/response logging with context

Installation

This package is part of the Pixielity Framework and is automatically available.

Configuration

Log Format

Set the log format in your .env file:

# Development (human-readable)
LOG_FORMAT=hybrid

# Production (machine-readable JSON)
LOG_FORMAT=json

HTTP Logging

Configure HTTP logging in config/http-logger.php:

return [
    'enabled' => env('HTTP_LOGGER_ENABLED', true),
    'log_writer' => \Pixielitygging\Writers\ContextAwareLogWriter::class,
    'log_channel' => env('LOG_CHANNEL', 'stack'),
    'log_level' => 'info',
];

Formatters

JSON Formatter (Production)

Single-line JSON format for log aggregators (ELK, Datadog, CloudWatch):

{
    "timestamp": "2026-02-03T10:56:48.000000Z",
    "level": "INFO",
    "channel": "local",
    "message": "POST /api/v1/test",
    "context": {
        "request_id": "abc123",
        "ip": "127.0.0.1",
        "method": "POST",
        "route": "api/v1/test",
        "locale": "ar",
        "execution_time_ms": 5.45
    }
}

Usage with jq:

# Pretty print
tail -f storage/logs/laravel.log | jq '.'

# Filter by correlation_id
cat storage/logs/laravel.log | jq 'select(.context.correlation_id == "trace-123")'

# Get slow requests
cat storage/logs/laravel.log | jq 'select(.context.execution_time_ms > 100)'

Hybrid Formatter (Development)

Human-readable format with structured context:

[2026-02-03 10:56:48] local.INFO: POST /api/v1/test
Context: {"request_id":"abc123","ip":"127.0.0.1","method":"POST","route":"api/v1/test","locale":"ar","execution_time_ms":5.45}

Log Context

All logs automatically include comprehensive context:

  • request_id - Unique request identifier
  • correlation_id - For distributed tracing
  • ip - Client IP address
  • method - HTTP method (GET, POST, etc.)
  • user_agent - Client user agent
  • route - Route name or path
  • session_id - Session identifier (if exists)
  • timezone - Client timezone
  • locale - Application locale
  • user_id - Authenticated user ID (if authenticated)
  • api_version - API version
  • execution_time_ms - Request execution time
  • memory_used_mb - Memory used by request

Middleware

HTTP Logger Middleware

Automatically logs all HTTP requests with full context:

#[Middleware(
    alias: 'http.logger',
    priority: 10
)]
class HttpLoggerMiddleware extends HttpLogger
{
    // Logs AFTER response is generated to include all context
}

The middleware is automatically registered as global middleware.

Writers

Context-Aware Log Writer

Custom log writer that includes Laravel's log context in HTTP logs:

class ContextAwareLogWriter implements LogWriter
{
    public function logRequest(Request $request): void
    {
        // Logs request with all context from Log::withContext()
    }
}

Usage Examples

Adding Custom Context

use Illuminate\Support\Facades\Log;

// Add context for all logs in this request
Log::withContext([
    'order_id' => $order->id,
    'payment_method' => 'stripe',
]);

// All subsequent logs will include this context
Log::info('Order processed');

Distributed Tracing

Send X-Correlation-ID header to trace requests across services:

curl -H "X-Correlation-ID: trace-abc-123" https://api.example.com/users

All logs for this request will include correlation_id: trace-abc-123.

Querying Logs

JSON Format:

# Find all requests for a specific user
cat storage/logs/laravel.log | jq 'select(.context.user_id == 42)'

# Find slow requests
cat storage/logs/laravel.log | jq 'select(.context.execution_time_ms > 1000)'

# Get all errors
cat storage/logs/laravel.log | jq 'select(.level == "ERROR")'

Hybrid Format:

# Find by request_id
grep "abc123" storage/logs/laravel.log

# Find by correlation_id
grep "trace-abc-123" storage/logs/laravel.log

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-09