Multiple loggers

451 views Asked by At

I'd like to create multiple loggers where different areas of my app will log to different files. For example, all the classes associated with getting the users data would log to a user.log, all functionality of making purchases going to a purchase.log. I am using the configuration array method for setting up the logger & appenders. In my index.php:

require_once('log4php/Logger.php');
require_once('classB.php');
require_once('classA.php');

 Logger::configure(array(
'rootLogger' => array('appenders' => array('default')),
'classALogger' => array('appenders' => array('classAAppender')),
'appenders' => array(
    'default' => array(
        'class' => 'LoggerAppenderEcho',
        'layout' => array(
            'class' => 'LoggerLayoutSimple'
            )
    ),'classAAppender' => array(
        'class' => 'LoggerAppenderFile',
        'additivity' => false,
        'layout' => array(
            'class' => 'LoggerLayoutSimple'
        ),
        'params' => array(
            'file' => 'log/classA.log',
            'append' => true
        )
    )
),

));

$logger = Logger::getLogger("main");
$logger->info('message from index' . '<br>');

$classA = new ClassA();
$classA->test();

Class A is as follows:

class ClassA
{   
    public function test()
    {
        $logger = Logger::getLogger("classALogger");

        $logger->error('from ClassA');
    }
}

I am able to log to the default or root logger but am not able to log to, in this example, classALogger. Any suggestions?

0

There are 0 answers