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
pixielity/laravel-logging 适用场景与选型建议
pixielity/laravel-logging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pixielity/laravel-logging 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pixielity/laravel-logging 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-09