Create Zend Server Monitor Event #2343

Closed
wants to merge 1 commit into from
View
36 library/Zend/Mvc/View/Http/ViewManager.php
@@ -83,6 +83,7 @@ class ViewManager implements ListenerAggregateInterface
protected $rendererStrategy;
protected $resolver;
protected $routeNotFoundStrategy;
+ protected $zendMonitorExceptionStrategy;
protected $view;
protected $viewModel;
/**@-*/
@@ -133,18 +134,20 @@ public function onBootstrap($event)
$this->services = $services;
$this->event = $event;
- $routeNotFoundStrategy = $this->getRouteNotFoundStrategy();
- $exceptionStrategy = $this->getExceptionStrategy();
- $mvcRenderingStrategy = $this->getMvcRenderingStrategy();
- $createViewModelListener = new CreateViewModelListener();
- $injectTemplateListener = new InjectTemplateListener();
- $injectViewModelListener = new InjectViewModelListener();
- $sendResponseListener = new SendResponseListener();
+ $routeNotFoundStrategy = $this->getRouteNotFoundStrategy();
+ $exceptionStrategy = $this->getExceptionStrategy();
+ $zendMonitorExceptionStrategy = $this->getZendMonitorExceptionStrategy();
+ $mvcRenderingStrategy = $this->getMvcRenderingStrategy();
+ $createViewModelListener = new CreateViewModelListener();
+ $injectTemplateListener = new InjectTemplateListener();
+ $injectViewModelListener = new InjectViewModelListener();
+ $sendResponseListener = new SendResponseListener();
$this->registerMvcRenderingStrategies($events);
$this->registerViewStrategies();
$events->attach($routeNotFoundStrategy);
+ $events->attach($zendMonitorExceptionStrategy);
@weierophinney
Zend Framework member

This is the wrong place to wire it. Since it has to do with dispatch logic, it should not be in the ViewManager.

I'd recommend creating a Logger service factory. Users could then call on that to attach additional writers, or to simply get the logger instance. We could then by default add a ZendMonitor log writer, and have filters attached to it that only log in certain situations. This could then be attached to specific events.

Why add a ZendMonitor LogWriter by default? I don't understand...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
$events->attach($exceptionStrategy);
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($injectViewModelListener, 'injectViewModel'), -100);
$events->attach($mvcRenderingStrategy);
@@ -376,6 +379,25 @@ public function getViewModel()
return $this->viewModel;
}
+
+
+ /**
+ * Instantiates and configures the Zend Monitor Exception strategy
+ * @return \Zend\Mvc\View\Http\ZendMonitorExceptionStrategy
+ */
+ public function getZendMonitorExceptionStrategy()
+ {
+ if ($this->zendMonitorExceptionStrategy) {
+ return $this->zendMonitorExceptionStrategy;
+ }
+
+ $this->zendMonitorExceptionStrategy = new ZendMonitorExceptionStrategy();
+
+ $this->services->setService('zendMonitorExceptionStrategy', $this->zendMonitorExceptionStrategy);
+ $this->services->setAlias('Zend\Mvc\View\Http\ZendMonitorExceptionStrategy', 'ZendMonitorExceptionStrategy');
+
+ return $this->zendMonitorExceptionStrategy;
+ }
/**
* Register additional mvc rendering strategies
View
118 library/Zend/Mvc/View/Http/ZendMonitorExceptionStrategy.php
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\View\Http;
@weierophinney
Zend Framework member

Again, this is not view related. I'd actually put it at the top-level of the Mvc hierarchy instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+use Zend\EventManager\EventManagerInterface;
+use Zend\EventManager\ListenerAggregateInterface;
+use Zend\Http\Response as HttpResponse;
+use Zend\Mvc\Application;
+use Zend\Mvc\MvcEvent;
+use Zend\Stdlib\ResponseInterface as Response;
+use Zend\View\Model\ViewModel;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage View
+ */
+class ZendMonitorExceptionStrategy implements ListenerAggregateInterface
+{
+ /**
+ * @var \Zend\Stdlib\CallbackHandler[]
+ */
+ protected $listeners = array();
+
+ /**
+ * Attach the aggregate to the specified event manager
+ *
+ * @param EventManagerInterface $events
+ * @return void
+ */
+ public function attach(EventManagerInterface $events)
+ {
+ $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'createCustomEvent'));
+ }
+
+ /**
+ * Detach aggregate listeners from the specified event manager
+ *
+ * @param EventManagerInterface $events
+ * @return void
+ */
+ public function detach(EventManagerInterface $events)
+ {
+ foreach ($this->listeners as $index => $listener) {
+ if ($events->detach($listener)) {
+ unset($this->listeners[$index]);
+ }
+ }
+ }
+
+ /**
+ * Create a custom event in Zend Server
+ *
+ * @param MvcEvent $e
+ * @return void
+ */
+ public function createCustomEvent(MvcEvent $e)
+ {
+ // Do nothing if no error in the event
+
+ $error = $e->getError();
+ if (empty($error)) {
+ return;
+ }
+
+ // Do nothing if the result is a response object
+ $result = $e->getResult();
+ if ($result instanceof Response) {
+ return;
+ }
+
+ switch ($error) {
+ case Application::ERROR_CONTROLLER_NOT_FOUND:
+ case Application::ERROR_CONTROLLER_INVALID:
+ case Application::ERROR_ROUTER_NO_MATCH:
+ // Specifically not handling these
+ return;
+
+ case Application::ERROR_EXCEPTION:
+ default:
+ if (!empty($e)) {
+ $exception = $e->getParam('exception'); /* @var $exception \Exception */
+ $message = 'An error occurred during execution: ' . $exception->getMessage();
+ if ($this->isCustomEventByRuleIsEnabled()) {
+ zend_monitor_custom_event_ex('Zend Framework Exception', $message, 'Zend Framework Exception Rule');
@weierophinney
Zend Framework member

Why isn't the ZendMonitor log writer being used here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ } elseif ($this->isCustomEventIsEnabled) {
+ zend_monitor_custom_event('Zend Framework Exception', $message);
+ }
+ }
+ }
+ }
+
+ /**
+ * Is Zend Server Monitor enabled?
+ * @return boolean
+ */
+ protected function isCustomEventByRuleIsEnabled()
+ {
+ return function_exists('zend_monitor_custom_event_ex');
+ }
+
+ /**
+ * Is Zend Server Monitor enabled?
+ * @return boolean
+ */
+ protected function isCustomEventIsEnabled()
+ {
+ return function_exists('zend_monitor_custom_event');
+ }
+}