ElasticaFormatter.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. use Elastica\Document;
  12. /**
  13. * Format a log message into an Elastica Document
  14. *
  15. * @author Jelle Vink <jelle.vink@gmail.com>
  16. */
  17. class ElasticaFormatter extends NormalizerFormatter
  18. {
  19. /**
  20. * @var string Elastic search index name
  21. */
  22. protected $index;
  23. /**
  24. * @var string Elastic search document type
  25. */
  26. protected $type;
  27. /**
  28. * @param string $index Elastic Search index name
  29. * @param string $type Elastic Search document type
  30. */
  31. public function __construct($index, $type)
  32. {
  33. // elasticsearch requires a ISO 8601 format date with optional millisecond precision.
  34. parent::__construct('Y-m-d\TH:i:s.uP');
  35. $this->index = $index;
  36. $this->type = $type;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function format(array $record)
  42. {
  43. $record = parent::format($record);
  44. return $this->getDocument($record);
  45. }
  46. /**
  47. * Getter index
  48. * @return string
  49. */
  50. public function getIndex()
  51. {
  52. return $this->index;
  53. }
  54. /**
  55. * Getter type
  56. * @return string
  57. */
  58. public function getType()
  59. {
  60. return $this->type;
  61. }
  62. /**
  63. * Convert a log message into an Elastica Document
  64. *
  65. * @param array $record Log message
  66. * @return Document
  67. */
  68. protected function getDocument($record)
  69. {
  70. $document = new Document();
  71. $document->setData($record);
  72. $document->setType($this->type);
  73. $document->setIndex($this->index);
  74. return $document;
  75. }
  76. }