LogstashFormatter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php declare(strict_types=1);
  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. /**
  12. * Serializes a log message to Logstash Event Format
  13. *
  14. * @see https://www.elastic.co/products/logstash
  15. * @see https://github.com/elastic/logstash/blob/master/logstash-core/src/main/java/org/logstash/Event.java
  16. *
  17. * @author Tim Mower <timothy.mower@gmail.com>
  18. */
  19. class LogstashFormatter extends NormalizerFormatter
  20. {
  21. /**
  22. * @var string the name of the system for the Logstash log message, used to fill the @source field
  23. */
  24. protected $systemName;
  25. /**
  26. * @var string an application name for the Logstash log message, used to fill the @type field
  27. */
  28. protected $applicationName;
  29. /**
  30. * @var string the key for 'extra' fields from the Monolog record
  31. */
  32. protected $extraKey;
  33. /**
  34. * @var string the key for 'context' fields from the Monolog record
  35. */
  36. protected $contextKey;
  37. /**
  38. * @param string $applicationName the application that sends the data, used as the "type" field of logstash
  39. * @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
  40. * @param string $extraKey the key for extra keys inside logstash "fields", defaults to extra
  41. * @param string $contextKey the key for context keys inside logstash "fields", defaults to context
  42. */
  43. public function __construct(string $applicationName, string $systemName = null, string $extraKey = 'extra', string $contextKey = 'context')
  44. {
  45. // logstash requires a ISO 8601 format date with optional millisecond precision.
  46. parent::__construct('Y-m-d\TH:i:s.uP');
  47. $this->systemName = $systemName ?: gethostname();
  48. $this->applicationName = $applicationName;
  49. $this->extraKey = $extraKey;
  50. $this->contextKey = $contextKey;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function format(array $record): string
  56. {
  57. $record = parent::format($record);
  58. if (empty($record['datetime'])) {
  59. $record['datetime'] = gmdate('c');
  60. }
  61. $message = [
  62. '@timestamp' => $record['datetime'],
  63. '@version' => 1,
  64. 'host' => $this->systemName,
  65. ];
  66. if (isset($record['message'])) {
  67. $message['message'] = $record['message'];
  68. }
  69. if (isset($record['channel'])) {
  70. $message['type'] = $record['channel'];
  71. $message['channel'] = $record['channel'];
  72. }
  73. if (isset($record['level_name'])) {
  74. $message['level'] = $record['level_name'];
  75. }
  76. if (isset($record['level'])) {
  77. $message['monolog_level'] = $record['level'];
  78. }
  79. if ($this->applicationName) {
  80. $message['type'] = $this->applicationName;
  81. }
  82. if (!empty($record['extra'])) {
  83. $message[$this->extraKey] = $record['extra'];
  84. }
  85. if (!empty($record['context'])) {
  86. $message[$this->contextKey] = $record['context'];
  87. }
  88. return $this->toJson($message) . "\n";
  89. }
  90. }