定制 memran/marwa-logger 二次开发

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

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

memran/marwa-logger

Composer 安装命令:

composer require memran/marwa-logger

包简介

Lightweight PSR-3 JSON logger for PHP with redaction, rotation, and retention.

README 文档

README

Packagist Version Packagist Downloads PHP PSR-3 CI PHPStan PHPUnit

Lightweight PSR-3 JSON logger for PHP 8.1+ with sensitive-data redaction, file rotation, and retention cleanup.

Features

  • ✅ PSR-3 compatible SimpleLogger
  • 🧾 NDJSON output for easy ingestion by log processors
  • 🔒 Recursive sensitive-data filtering
  • 🚦 Production gating by log level and request origin
  • 🗂️ File rotation by size
  • ♻️ Optional retention by age and file count
  • 🧪 Composer scripts, PHPUnit, PHPStan, and CI included

📦 Installation

composer require memran/marwa-logger

🚀 Quick Start

<?php

require __DIR__ . '/vendor/autoload.php';

use Marwa\Logger\Logger;

$logger = Logger::boot(
    channel: 'payments',
    env: 'prod',
    path: __DIR__ . '/storage/logs',
    size: '10MB',
);

$logger->info('checkout_started', ['user_id' => 42]);
$logger->error('payment_failed', ['_origin' => 'system', 'order_id' => 991]);

Logger::boot() defaults to APP_ENV and storage/logs when env and path are omitted.

⚙️ Advanced File Sink Configuration

Use StorageFactory when you need retention controls:

use Marwa\Logger\SimpleLogger;
use Marwa\Logger\Storage\StorageFactory;
use Marwa\Logger\Support\SensitiveDataFilter;
use Psr\Log\LogLevel;

$sink = StorageFactory::make([
    'driver' => 'file',
    'path' => __DIR__ . '/storage/logs',
    'prefix' => 'myapp',
    'max_bytes' => '10MB',
    'retention_days' => 7,
    'max_files' => 30,
]);

$logger = new SimpleLogger(
    appName: 'myapp',
    env: 'production',
    sink: $sink,
    filter: new SensitiveDataFilter(),
    logging: true,
    productionMinLevel: LogLevel::ERROR,
);

🧠 Runtime Behavior

In production or prod, user-origin logs are skipped unless ['_origin' => 'system'] is set. Levels below productionMinLevel are ignored. Sensitive keys such as password, token, authorization, and client_secret are redacted recursively.

Log records include timestamps, request metadata, PHP runtime details, message text, request IDs, and scrubbed context. Invalid UTF-8 is sanitized before encoding so log writes do not silently fail.

🗃️ File Rotation and Retention

Active files use the pattern prefix-YYYY-MM-DD.log. When max_bytes is exceeded, the current file is renamed to prefix-YYYY-MM-DD_HHMMSS-<token>.log and a fresh active file is created. If configured, retention_days deletes expired matching files and max_files keeps only the newest matching log files.

🚨 Error Handling

The file sink throws RuntimeException when it cannot create the log directory, rotate a file, or append a record. Treat these as operational failures and surface them in deployment monitoring.

🛠️ Development

composer lint
composer analyze
composer test
composer check

composer check runs syntax checks, PHPStan 2.x, and PHPUnit.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固