定制 xsga/log4php 二次开发

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

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

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

PHP PSR-3 License: Apache 2.0

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

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:

👥 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2026-04-19