xsga/log4php
Composer 安装命令:
composer require xsga/log4php
包简介
A PHP logging library based on Apache Log4PHP, PSR-3 compliant, supporting multiple appenders, layouts and log levels.
README 文档
README
A modern, PSR-compliant successor to Apache Log4PHP (now unmaintained).
Xsga Log4PHP is a heavily refactored and redesigned logging library for PHP, originally inspired by Apache Log4PHP and rebuilt for the current PHP ecosystem. It targets PHP 8.4+, follows PSR-3, ships with PSR-4 autoloading, and provides a flexible logging pipeline with appenders, layouts, context interpolation, and XML or programmatic configuration.
Table of Contents
- Why this project exists
- Key Features
- What makes it different
- Quick Start
- Basic Configuration
- Documentation
- Who should use this
- License
- Acknowledgements
📌 Why this project exists
Apache Log4PHP is no longer actively maintained and does not align with current PHP versions, tooling, or interoperability expectations.
This project exists to:
- Provide a conceptual successor for teams familiar with Apache Log4PHP
- Preserve the strengths of the original model while removing outdated constraints
- Modernize the architecture, code style, and runtime requirements
- Align the library with current PHP standards, especially PSR-3 and PSR-4
- Offer a maintainable codebase suitable for static analysis, code style checks, and ongoing development
✨ Key Features
- ✅ PSR-3 compatible logging interface and context placeholder interpolation
- ✅ PSR-4 autoloading support via Composer
- ✅ PHP 8.4+ compatibility
- ✅ XML-based and programmatic array-based configuration
- ✅ Multiple appenders, including File, DailyFile, RollingFile, Console, and Loki
- ✅ Multiple layouts, including Pattern, JSON, and Simple
- ✅ Full log level support from ALL through OFF, aligned with modern logging expectations
- ✅ Refactored architecture with cleaner separation of responsibilities
- ✅ Improved maintainability, extensibility, and static-analysis friendliness
🔍 What makes it different
Compared to the original Apache Log4PHP:
- The internals have been substantially refactored for modern PHP and stricter typing expectations
- The package is distributed as a Composer-first library with PSR-4 autoloading
- Logging follows the PSR-3 method set and context conventions
- Configuration remains familiar, but the implementation is cleaner and easier to maintain
- Appenders and layouts are designed for present-day use cases, including structured JSON output and Loki integration
- Development tooling now includes linting, PSR-12 style checks, and Psalm-based static analysis
For a detailed comparison, see:
👉 doc/differences-from-apache-log4php.md
🚀 Quick Start
Install via Composer:
composer require xsga/log4php
Basic usage:
use Xsga\Log4Php\Logger; // Load configuration from an XML file. Logger::configure('path/to/log4php.xml'); // Obtain a named logger. $logger = Logger::getLogger('MyApp'); $logger->info('Application started.'); $logger->warning('Disk space running low.', ['threshold' => '90%']); // Or use the root logger. $root = Logger::getRootLogger(); $root->debug('Root logger is ready.');
If you prefer to configure the library without XML, you can also pass a PHP array directly to Logger::configure().
⚙️ Basic Configuration
Configuration can be defined using XML or arrays.
XML example:
<?xml version="1.0" encoding="UTF-8" ?> <configuration xmlns="http://logging.apache.org/log4php/" threshold="all"> <!-- File appender with pattern layout: one line per event --> <appender name="default" class="LoggerAppenderFile"> <param name="file" value="logs/app.log" /> <layout class="LoggerLayoutPattern"> <param name="conversionPattern" value="%date{Y-m-d H:i:s} [%-5level] %message%newline" /> </layout> </appender> <root> <level value="debug" /> <appender_ref ref="default" /> </root> </configuration>
Programmatic example:
use Xsga\Log4Php\Logger; Logger::configure([ 'appenders' => [ 'default' => [ 'class' => 'LoggerAppenderFile', 'params' => [ 'file' => 'logs/app.log', ], 'layout' => [ 'class' => 'LoggerLayoutPattern', ], ], ], 'rootLogger' => [ 'level' => 'debug', 'appenders' => ['default'], ], ]);
📚 Documentation
Project documentation is currently centered on the repository itself:
- Differences from Apache Log4PHP: doc/differences-from-apache-log4php.md
- Project documentation: doc/DOCUMENTATION.md
👥 Who should use this
This project is a good fit if you:
- Are currently using Apache Log4PHP and need a modern successor with a familiar mental model
- Need a PHP 8.4+ logging library with PSR-3 semantics
- Want XML-driven logging configuration without giving up programmatic configuration options
- Need appenders for local files, rolling files, console output, or Grafana Loki
- Prefer a lightweight logging library over larger framework-coupled solutions
📄 License
This project is based on Apache Log4PHP and remains licensed under the Apache 2.0 License. See LICENSE for details.
It includes substantial refactoring, modernization, and new implementation work tailored to the current PHP ecosystem.
🙌 Acknowledgements
- Apache Log4PHP
- Apache Software Foundation
xsga/log4php 适用场景与选型建议
xsga/log4php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「logger」 「psr-3」 「log4php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xsga/log4php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xsga/log4php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xsga/log4php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module that sets up Monolog for logging in applications.
Workflow logger
HTTP request logger middleware for Laravel
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 45
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-04-19