initphp/logger 问题修复 & 功能扩展

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

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

initphp/logger

Composer 安装命令:

composer require initphp/logger

包简介

PSR-3 compliant logger with File and PDO handlers, plus a multiplexer for sending each log record to several handlers at once.

README 文档

README

A small, focused, PSR-3 compliant logger for PHP 8.0+. Ships with two built-in handlers and a tiny multiplexer that fans every log record out to several handlers at once.

Latest Stable Version Total Downloads License PHP Version Require

At a glance

  • FileLogger — appends each record as a single line to a file, with optional date-token placeholders in the path ({year}/{month}/{day}/...).
  • PDOLogger — inserts each record as a row in a relational table, using prepared statements. Works with any PDO driver (MySQL, PostgreSQL, SQLite…).
  • Logger — accepts any number of Psr\Log\LoggerInterface instances and forwards every call to all of them, in registration order.

Everything implements Psr\Log\LoggerInterface (PSR-3 v3), so you can drop the package into any framework or library that consumes that contract — or compose it with handlers from other PSR-3 packages.

Requirements

Requirement Version
PHP >= 8.0
psr/log ^3.0
ext-pdo Required only for PDOLogger

Installation

composer require initphp/logger

Quick start

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

use InitPHP\Logger\FileLogger;
use InitPHP\Logger\Logger;

$logger = new Logger(
    new FileLogger(['path' => __DIR__ . '/logs/app.log'])
);

$logger->info('Service booted in {ms}ms', ['ms' => 42]);
$logger->error('Payment {id} failed', ['id' => 9182]);

Produces, for example:

2026-05-24T14:08:22+03:00 [INFO] Service booted in 42ms
2026-05-24T14:08:22+03:00 [ERROR] Payment 9182 failed

Handlers

FileLogger

use InitPHP\Logger\FileLogger;

$logger = new FileLogger([
    'path' => __DIR__ . '/logs/app-{year}-{month}-{day}.log',
]);

Path tokens ({year}, {month}, {day}, {hour}, {minute}, {second}) are resolved once, at construction time, against the process clock. The parent directory is created automatically (mode 0775) if it does not already exist. Writes use FILE_APPEND | LOCK_EX, so concurrent processes do not interleave bytes within a single record.

Full reference: docs/02-file-logger.md.

PDOLogger

use InitPHP\Logger\PDOLogger;

$pdo = new PDO('mysql:host=localhost;dbname=app;charset=utf8mb4', 'app', 'secret');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$logger = new PDOLogger(['pdo' => $pdo, 'table' => 'logs']);

$logger->error('User {user} caused an error.', ['user' => 'muhammetsafak']);
// INSERT INTO logs (level, message, date) VALUES ('ERROR', 'User muhammetsafak caused an error.', '2026-05-24 14:08:22')

Reference DDL for MySQL:

CREATE TABLE `logs` (
    `id`      BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `level`   ENUM('EMERGENCY','ALERT','CRITICAL','ERROR','WARNING','NOTICE','INFO','DEBUG') NOT NULL,
    `message` TEXT NOT NULL,
    `date`    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY `idx_logs_level_date` (`level`, `date`)
) ENGINE = InnoDB CHARSET = utf8mb4 COLLATE utf8mb4_general_ci;

PostgreSQL and SQLite variants are documented in docs/03-pdo-logger.md.

Multi-handler logging

use InitPHP\Logger\FileLogger;
use InitPHP\Logger\Logger;
use InitPHP\Logger\PDOLogger;

$logger = new Logger(
    new FileLogger(['path' => __DIR__ . '/logs/app.log']),
    new PDOLogger(['pdo' => $pdo, 'table' => 'logs'])
);

$logger->warning('cache miss for key {key}', ['key' => 'user:42']);
// Goes to BOTH the file and the database table.

Logger accepts any Psr\Log\LoggerInterface, including handlers from other PSR-3 packages — see docs/05-custom-handlers.md.

PSR-3 surface

Every handler exposes the full PSR-3 API:

$logger->emergency(string|\Stringable $message, array $context = []): void;
$logger->alert    (string|\Stringable $message, array $context = []): void;
$logger->critical (string|\Stringable $message, array $context = []): void;
$logger->error    (string|\Stringable $message, array $context = []): void;
$logger->warning  (string|\Stringable $message, array $context = []): void;
$logger->notice   (string|\Stringable $message, array $context = []): void;
$logger->info     (string|\Stringable $message, array $context = []): void;
$logger->debug    (string|\Stringable $message, array $context = []): void;
$logger->log      ($level, string|\Stringable $message, array $context = []): void;

Context placeholders ({name}) are expanded with the matching key from $context. Booleans render as true / false, null renders as the empty string, \Throwable values render as Class(code): message in file:line, and anything implementing __toString() is cast to string. Arrays and non-stringable objects are skipped — see docs/06-psr3-context.md.

Unknown log levels throw Psr\Log\InvalidArgumentException, per PSR-3 §1.1.

Documentation

Topic-by-topic guides live in docs/:

Testing the package itself

composer install
composer test         # PHPUnit
composer phpstan      # PHPStan (level max)
composer cs-check     # PHP-CS-Fixer dry-run
composer ci           # all of the above

Contributing

Please read the org-wide CONTRIBUTING.md before opening a pull request. Bug reports and feature ideas go through Issues and Discussions.

Security

If you discover a security vulnerability, please follow the instructions in the org-wide SECURITY.md. Do not open a public issue.

License

Released under the MIT License. Copyright © InitPHP.

initphp/logger 适用场景与选型建议

initphp/logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「logging」 「logger」 「psr-3」 「PSR3」 「file-logger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 initphp/logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 initphp/logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 initphp/logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-15