LogglyFormatter.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. * Encodes message information into JSON in a format compatible with Loggly.
  13. *
  14. * @author Adam Pancutt <adam@pancutt.com>
  15. */
  16. class LogglyFormatter extends JsonFormatter
  17. {
  18. /**
  19. * Overrides the default batch mode to new lines for compatibility with the
  20. * Loggly bulk API.
  21. */
  22. public function __construct(int $batchMode = self::BATCH_MODE_NEWLINES, bool $appendNewline = false)
  23. {
  24. parent::__construct($batchMode, $appendNewline);
  25. }
  26. /**
  27. * Appends the 'timestamp' parameter for indexing by Loggly.
  28. *
  29. * @see https://www.loggly.com/docs/automated-parsing/#json
  30. * @see \Monolog\Formatter\JsonFormatter::format()
  31. */
  32. public function format(array $record): string
  33. {
  34. if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTimeInterface)) {
  35. $record["timestamp"] = $record["datetime"]->format("Y-m-d\TH:i:s.uO");
  36. unset($record["datetime"]);
  37. }
  38. return parent::format($record);
  39. }
  40. }