浏览代码

Se actualizo la definicion del servicio monolog handler

Guillermo Espinoza 8 年之前
父节点
当前提交
f801d90020
共有 2 个文件被更改,包括 36 次插入21 次删除
  1. 1 1
      Resources/config/services.yml
  2. 35 20
      Services/MonologHandler.php

+ 1 - 1
Resources/config/services.yml

@@ -9,7 +9,7 @@ monolog:
 services:
     monolog.devicelog_handler:
         class: DeviceBundle\Services\MonologHandler
-        arguments: ['@webservice','%remote_device_url%','%remote_device_log_url%']
+        arguments: ['@service_container']
     
     device.device_validator:
         class: DeviceBundle\Validator\Constraints\DeviceValidator

+ 35 - 20
Services/MonologHandler.php

@@ -5,6 +5,7 @@ namespace DeviceBundle\Services;
 use Buzz\Message\RequestInterface as HttpRequestInterface;
 use Monolog\Handler\AbstractProcessingHandler;
 use WebserviceBundle\Services\Webservice;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 class MonologHandler extends AbstractProcessingHandler
 {
@@ -12,30 +13,40 @@ class MonologHandler extends AbstractProcessingHandler
     /**
      * @var Webservice
      */
-    protected $webservice;
-    
+    protected $webservice = null;
+
     /**
      * @var string
      */
-    protected $deviceGetUrl;
+    protected $deviceGetUrl = null;
 
     /**
      * @var string
      */
-    protected $deviceLogUrl;
+    protected $deviceLogUrl = null;
+
+    /**
+     * @var ContainerInterface
+     */
+    protected $serviceContainer;
 
 
     /**
-     * @param Webservice $webservice
-     * @param string $deviceGetUrl
-     * @param string $deviceLogUrl
+     * @param ContainerInterface $serviceContainer
      */
-    public function __construct(Webservice $webservice, $deviceGetUrl, $deviceLogUrl)
+    public function __construct(ContainerInterface $serviceContainer)
     {
         parent::__construct();
-        $this->webservice = $webservice;
-        $this->deviceGetUrl = $deviceGetUrl;
-        $this->deviceLogUrl = $deviceLogUrl;
+        $this->serviceContainer = $serviceContainer;
+        if ($serviceContainer->has('webservice')) {
+            $this->webservice = $serviceContainer->get('webservice');
+        }
+        if ($serviceContainer->hasParameter('remote_device_url')) {
+            $this->deviceGetUrl = $serviceContainer->getParameter('remote_device_url');
+        }
+        if ($serviceContainer->hasParameter('remote_device_log_url')) {
+            $this->deviceLogUrl = $serviceContainer->getParameter('remote_device_log_url');
+        }
     }
 
     /**
@@ -45,15 +56,19 @@ class MonologHandler extends AbstractProcessingHandler
      */
     protected function write(array $record)
     {
-        $filters = array(
-            'deviceType' => $record['context']['deviceType'],
-            'deviceId' => $record['context']['deviceId'],
-        );
-        $device = $this->webservice->get($this->deviceGetUrl, $filters);
-        if ($device) {
-            $data['device'] = $device[0]['id'];
-            $data['message'] = $record['formatted'];
-            $result = $this->webservice->makeRequest($this->deviceLogUrl, HttpRequestInterface::METHOD_POST, $data);
+        try {
+            $filters = array(
+                'deviceType' => $record['context']['deviceType'],
+                'deviceId' => $record['context']['deviceId'],
+            );
+            $device = $this->webservice->get($this->deviceGetUrl, $filters);
+            if ($device) {
+                $data['device'] = $device[0]['id'];
+                $data['message'] = $record['formatted'];
+                $this->webservice->makeRequest($this->deviceLogUrl, HttpRequestInterface::METHOD_POST, $data);
+            }
+        } catch (\Exception $ex) {
+
         }
     }