MonologHandler.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace DeviceBundle\Services;
  3. use Buzz\Message\RequestInterface as HttpRequestInterface;
  4. use Monolog\Handler\AbstractProcessingHandler;
  5. use WebserviceBundle\Services\Webservice;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. class MonologHandler extends AbstractProcessingHandler
  8. {
  9. /**
  10. * @var Webservice
  11. */
  12. protected $webservice = null;
  13. /**
  14. * @var string
  15. */
  16. protected $deviceGetUrl = null;
  17. /**
  18. * @var string
  19. */
  20. protected $deviceLogUrl = null;
  21. /**
  22. * @var ContainerInterface
  23. */
  24. protected $serviceContainer;
  25. /**
  26. * @param ContainerInterface $serviceContainer
  27. */
  28. public function __construct(ContainerInterface $serviceContainer)
  29. {
  30. parent::__construct();
  31. $this->serviceContainer = $serviceContainer;
  32. if ($serviceContainer->has('webservice')) {
  33. $this->webservice = $serviceContainer->get('webservice');
  34. }
  35. if ($serviceContainer->hasParameter('remote_device_url')) {
  36. $this->deviceGetUrl = $serviceContainer->getParameter('remote_device_url');
  37. }
  38. if ($serviceContainer->hasParameter('remote_device_log_url')) {
  39. $this->deviceLogUrl = $serviceContainer->getParameter('remote_device_log_url');
  40. }
  41. }
  42. /**
  43. * Crea un DeviceLog por REST en la app Base
  44. *
  45. * @param array $record
  46. */
  47. protected function write(array $record)
  48. {
  49. try {
  50. $filters = array(
  51. 'deviceType' => $record['context']['deviceType'],
  52. 'deviceId' => $record['context']['deviceId'],
  53. );
  54. $device = $this->webservice->get($this->deviceGetUrl, $filters);
  55. if ($device) {
  56. $data['device'] = $device[0]['id'];
  57. $data['message'] = $record['formatted'];
  58. $this->webservice->makeRequest($this->deviceLogUrl, HttpRequestInterface::METHOD_POST, $data);
  59. }
  60. } catch (\Exception $ex) {
  61. }
  62. }
  63. }