0

I try to create log file with Zend Framework 2. I found the solution by using Zend\Log\Logger and Zend\Log\Writer\Stream. In fact, it works fine. But the problem is that I need to use that code in every action method and in every controller class.

Does anybody know how to create log file for the whole website WITHOUT writing the same code in every action method and in every controller class? Thank you for your answer!

O Connor
  • 4,236
  • 15
  • 50
  • 91

1 Answers1

2

You would use the EventManager for this kind of feature. Anytime there's an error you'd trigger a specific Error-Event which you define. Then you have your LogingModule which would hook in to each and every Event you want to Log.

You'd have to pass the Exception/ErrorMessage into the Event of course.

TL/DR the EventManager is what you should be looking for.

Sam
  • 16,435
  • 6
  • 55
  • 89
  • This is how I did it -- $eventManager = $e->getApplication()->getEventManager(); $eventManager->attach('dispatch.error', function($e){ $exception = $e->getResult()->exception; if($exception) { $sm = $e->getApplication()->getServiceManager(); $service = $sm->get('ApplicationServiceErrorHandling'); $service->logException($exception); } }); Is it what you mean? – O Connor Mar 05 '14 at 11:48
  • Roughly, you won't attach to "dispatch.error" though, you'd rather hook in to the events that YOUR Modules provide. – Sam Mar 05 '14 at 11:59