2

i have a logger in my config like this:

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/err.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 3

But i want to use it for only error logs. For another logs wanna use another log file. For that reason i added these lines to app.ini

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream[] = APPLICATION_PATH "/../logs/debug.log"
resources.log.stream.writerParams.stream[] = APPLICATION_PATH "/../logs/info.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority[] = 7
resources.log.stream.filterParams.priority[] = 5

But this is not working i wanna use differetn files for different priorities. But logger overrides the first logger. How ?

duzenz
  • 331
  • 1
  • 4
  • 9

2 Answers2

8

Example of application.ini:

resources.log.err.writerName = "Stream"
resources.log.err.writerParams.stream =  APPLICATION_PATH "/../data/logs/ERR.log"
;resources.log.stream.formatterParams.format = "%priority%:%message% %timestamp% %priorityName%  %info% PHP_EOL"
resources.log.err.filterName = "Priority"
resources.log.err.filterParams.priority = 3
resources.log.err.filterParams.operator = "=="

resources.log.warn.writerName = "Stream"
resources.log.warn.writerParams.stream = APPLICATION_PATH "/../data/logs/WARN.log"
;resources.log.warn.formatterParams.format = "%priority%:%message%:%ip%:%userid% %timestamp% %priorityName%  %info% PHP_EOL"
resources.log.warn.filterName = "Priority"
resources.log.warn.filterParams.priority = 4
resources.log.warn.filterParams.operator = "=="

In bootstrap:

protected function _initLog() {
    $options = $this->getOption('resources');
    $partitionConfig = $this->getOption('log');
    $logOptions = $options['log'];
    $logger = Zend_Log::factory($logOptions);
    $logger->addPriority('USERACTION', 8);
    $logger->addPriority('DBLOG', 9);
    Zend_Registry::set('logger', $logger);
}

Then in codes:

$this->logger = Zend_Registry::get('logger');
$this->logger->setEventItem('ip', $_SERVER['REMOTE_ADDR']);
$this->logger->setEventItem('userid', $this->userId);

Use this way:

$this->logger->log('Test error', Zend_Log::WARN);

Or this way:

$this->logger->warn('Test error');

Or this way:

$this->logger->log('Test error', 4);
AlexWerz
  • 739
  • 2
  • 9
  • 21
Olivier
  • 96
  • 1
  • 3
  • Note that the constant PHP_EOL in application.ini must appear outside of the quotation marks. Otherwise it'll be interpreted as a literal. – Lars Nyström Feb 14 '14 at 11:12
1

You can use filters to accomplish this. Just let higher priority events pass through the filters until you want to log them.

http://framework.zend.com/manual/en/zend.log.filters.html

What's wrong in your setup is that you are trying to use the same writer which you should not. Use the filters to choose the writers and associate certain log files with certain writers, as shown in the second part of the example.

I think something like that should do the trick:

resources.log.stream.writerName = "myDebugWriter"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/debug.log"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::WARN
resources.log.stream.writerName = "myInfoWriter"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/info.log"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::INFO
markus
  • 40,136
  • 23
  • 97
  • 142
  • ok i get the writerName must be Stream or Firebug etc. But i can't how can i solve the problem of different files for different error levels. In Bootstrap i can do that but i wanna do that in app.ini file. – duzenz Sep 07 '11 at 14:40
  • the above code not work i tried it the problem is writerName parameters specified in ZF like Stream or Firebug. so "MyDebugWriter" is not working. i have to use "Stream" in my project because it support err log or debug log but i cant find anywhere how can i use in app.ini for different files – duzenz Sep 08 '11 at 07:28