laikmosh/plog 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

laikmosh/plog

最新稳定版本:1.0.8

Composer 安装命令:

composer require laikmosh/plog

包简介

Advanced Laravel logging system with metadata capture, tagging, and powerful filtering

README 文档

README

Plog is a powerful Laravel logging enhancement package that captures extensive metadata, supports tagging, and provides an interactive web interface for log exploration.

Features

  • Automatic Metadata Capture: User ID, Session ID, Request ID, file/line, class/method
  • Request Tracking: Track logs across HTTP requests, queued jobs, and CLI commands
  • Tagging System: Organize logs with tags for easy filtering
  • Interactive Web Interface: Filter, search, and explore logs with Livewire + Alpine.js
  • Flexible Storage: SQLite by default, configurable to any Laravel database
  • Granular Retention: Configure different retention periods for different log types

Installation

composer require laikmosh/plog

Configuration

Publish the configuration file and assets:

php artisan vendor:publish --tag=plog-config
php artisan vendor:publish --tag=plog-assets

Run migrations:

php artisan migrate

Environment Variables

# Enable/disable Plog
PLOG_ENABLED=true

# Authorized emails (comma-separated)
PLOG_AUTHORIZED_EMAILS=admin@example.com,developer@example.com

# Database connection (optional, defaults to SQLite)
PLOG_DB_CONNECTION=plog

# Default retention period
PLOG_RETENTION_DAYS=7

# Enable automatic cleanup
PLOG_CLEANUP_ENABLED=true

Usage

Basic Logging

All existing Laravel log calls automatically capture metadata:

Log::info('User logged in', ['user_id' => $user->id]);
Log::error('Payment failed', ['order_id' => $orderId]);

Using Tags

You can add tags to organize and filter your logs. Tags work seamlessly with Laravel's standard Log facade:

use Illuminate\Support\Facades\Log;

// Add tags using the special '_tags' key in the context array
Log::info('Order processed', [
    'order_id' => $orderId,
    '_tags' => ['payment', 'stripe']
]);

Log::error('Connection failed', ['_tags' => ['database', 'error']]);

Log::warning('Slow query', [
    'time' => 2.5,
    '_tags' => ['performance', 'database']
]);

// The _tags key is automatically extracted and stored separately
// It won't appear in your logged context data

This approach works with ALL Laravel log methods and doesn't require any changes to existing code. Just add '_tags' => ['tag1', 'tag2'] to your context array when needed.

Viewing Logs

Access the web interface at /logs (requires authentication and authorization).

The interface allows you to:

  • Filter by level, user, request ID, session, environment, endpoint, and tags
  • Search through log messages and context
  • Click any field to instantly filter by that value
  • View detailed log entries with full context
  • Group logs by request to trace execution flow

Advanced Configuration

Custom Database Connection

In config/plog.php:

'database' => [
    'connection' => 'mysql', // Use your app's main database
    'table' => 'plog_entries',
],

Retention Policies

Configure granular retention rules:

'retention' => [
    'default_days' => 7,
    'rules' => [
        ['tags' => ['payment'], 'days' => 30],
        ['tags' => ['authentication'], 'days' => 90],
        ['level' => 'error', 'days' => 14],
    ],
],

Authorization

Control access via email whitelist:

'authorized_emails' => [
    'admin@example.com',
    'developer@example.com',
],

Or customize the gate in your AuthServiceProvider:

Gate::define('viewPlog', function ($user) {
    return $user->hasRole('admin');
});

Request ID Tracking

Plog automatically generates and tracks request IDs across:

  • HTTP requests
  • Queued jobs (preserves original request ID)
  • CLI commands

Access the current request ID:

use Laikmosh\Plog\Services\RequestIdService;

$requestId = app(RequestIdService::class)->getRequestId();

Captured Metadata

Each log entry captures:

  • Time: Timestamp with microseconds
  • Level: debug, info, notice, warning, error, critical, alert, emergency
  • Message: Log message
  • Context: Additional data passed to the log
  • User ID: Currently authenticated user
  • Session ID: Current session identifier
  • Request ID: Unique request identifier
  • Environment: http, cli, queue, testing
  • Endpoint: Route name or URI, CLI command
  • File & Line: Source code location
  • Class & Method: Calling class and method
  • Tags: Custom tags for organization

Performance Considerations

  • Logs are written synchronously by default
  • Consider using a dedicated database for high-volume applications
  • Indexes are automatically created for common query patterns
  • Use retention policies to manage database size

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-29