Sử dụng log4php

First, install Composer if you don’t yet have it:

php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"

Create a composer.json file with the following content:

{
    "require": {
        "apache/log4php": "2.3.0"
    }
}

Run the Composer install procedure:

php composer.phar install

This will install Apache log4php in vendor/apache/log4php.

To use log4php simply include vendor/autoload.php in your script.

require 'vendor/autoload.php';
$log = Logger::getLogger("foo");
$log->info("Yay! Works!"); 

=====
File config: log4php.config.xml
<configuration xmlns="http://logging.apache.org/log4php/">

    <appender name="myConsoleAppender" class="LoggerAppenderConsole" />

    <appender name="myFileAppender" class="LoggerAppenderFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date [%logger] %message%newline" />
        </layout>
        <param name="file" value="logs/InnoMigrationApp.log" />
    </appender>

    <logger name="InnoMigrationApp">
        <appender_ref ref="myFileAppender" />
    </logger>

    <root>
        <level value="DEBUG" />
        <appender_ref ref="myConsoleAppender" />
    </root>
</configuration>

 

File php test:

<?php

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

global $log;
Logger::configure('log4php.config.xml');
$log = Logger::getLogger('InnoMigrationApp');

$log->debug("Yay! Works!");
exit();

Leave a Reply

You must be logged in to post a comment.