Search by

simova-core / log4php

simova-core

A versatile logging framework for PHP. Forked from apache/log4php

1.0.0 2026-02-03 21:11 UTC

This package is auto-updated.

Last update: 2026-08-26 19:30:44 UTC


README

A versatile logging framework for PHP, forked from Apache log4php (which is no longer maintained). This fork modernizes the library to run cleanly on PHP 8.3, removes deprecated constructs, fixes compatibility bugs, and adds performance options for use in larger applications.

Requirements

  • PHP ^8.3
  • Extensions: ext-libxml, ext-simplexml, ext-pdo
  • Optional (only for the MongoDB appender): ext-mongodb and the mongodb/mongodb library ^2.0

Installation

composer require simova-core/log4php

The MongoDB appender is optional. If you use it, also install:

composer require mongodb/mongodb:^2.0

Basic usage

<?php
require_once 'vendor/autoload.php';

$logger = Logger::getLogger('myLogger');

$logger->info('Application started');
$logger->warn('Something looks off');
$logger->error('An error occurred', new Exception('boom'));

Without explicit configuration, log4php uses a default configuration (echo appender, DEBUG level). You can configure it with an array, or an XML/INI/PHP file:

Logger::configure([
    'rootLogger' => [
        'level'     => 'INFO',
        'appenders' => ['file'],
    ],
    'appenders' => [
        'file' => [
            'class'  => 'LoggerAppenderFile',
            'layout' => ['class' => 'LoggerLayoutPattern'],
            'params' => [
                'file' => '/var/log/app.log',
            ],
        ],
    ],
]);

Appenders

The bundled appenders include: Console, Echo, File, DailyFile, RollingFile, Mail, MailEvent, Null, Php, PDO, Socket, Syslog, FirePHP and MongoDB.

File appender performance options

For high-volume logging in larger projects, LoggerAppenderFile (and its subclasses LoggerAppenderDailyFile and LoggerAppenderRollingFile) support two options:

  • bufferSize (int, default 0) - Number of messages to accumulate before flushing to disk. 0 keeps the legacy behavior (immediate write). Values greater than 0 reduce the number of I/O syscalls and lock acquisitions, which improves throughput. The buffer is flushed on close() and on the object destructor.
  • locking (bool, default true) - Whether to lock the file (flock) before each write. Disable it when concurrent access is not a concern to gain performance.
'appenders' => [
    'file' => [
        'class'  => 'LoggerAppenderFile',
        'layout' => ['class' => 'LoggerLayoutSimple'],
        'params' => [
            'file'       => '/var/log/app.log',
            'bufferSize' => 100,   // flush every 100 events
            'locking'    => false, // skip flock for extra speed
        ],
    ],
],

MongoDB appender

The MongoDB appender was rewritten to use the modern mongodb/mongodb library (^2.0) and the ext-mongodb driver. Authentication is handled through the connection URI options.

Configurable parameters: host, port, databaseName, collectionName, userName, password, timeout (in milliseconds).

Running the tests

Install the dev dependencies and run PHPUnit:

composer install
composer test

The test suite targets PHPUnit ^10.5 || ^11.0. Tests are grouped and can be run individually:

vendor/bin/phpunit --group appenders
vendor/bin/phpunit --group configurators

MongoDB appender tests are skipped automatically when ext-mongodb / mongodb/mongodb are not available.

Documentation

The original Apache log4php documentation still applies to most of the API: http://logging.apache.org/log4php/

See CHANGELOG for the changes introduced by this fork.

License

Apache License, Version 2.0.