We are using zend framework 2 for a new application, i would like to have the same logging system of Rails or similar, i would like have a log for each request, is possible to do this in Zend?
Log each request in ZF2
1.8k Views Asked by Mauro At
2
There are 2 best solutions below
0

Sounds like you could attach a listener to the dispatch
event on Zend\Mvc\Application
.
For reference, Rob Allen has created a handy list of ZF2 events.
It depends what you want to log. If it is just an access log, you should try to use the webserver's log. The logs from Apache/nginx/IIS etc perform better than you will achieve in your ZF2 app.
If you need to log inside the ZF2 application, you have two choices. First option is at
bootstrap
. It's one of the earliest options you can use, so probably therefore the best. However, you can also look atroute
ordispatch
. Those two events are called during the "run" phase of the application. With these events, you have for example a route match available and therefore you know (or not) if your request did match any controller (or in case you don't have the match, it's a 404).Some examples. Let's assume you have a logger configured in the
ServiceManager
under thelogger
key. Then to log atbootstrap
:Or for example if you wait for
route
, you attach a listener for theroute
event: