No Description

LogglyFormatter.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /**
  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. * @param int $batchMode
  23. */
  24. public function __construct($batchMode = self::BATCH_MODE_NEWLINES, $appendNewline = false)
  25. {
  26. parent::__construct($batchMode, $appendNewline);
  27. }
  28. /**
  29. * Appends the 'timestamp' parameter for indexing by Loggly.
  30. *
  31. * @see https://www.loggly.com/docs/automated-parsing/#json
  32. * @see \Monolog\Formatter\JsonFormatter::format()
  33. */
  34. public function format(array $record)
  35. {
  36. if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTime)) {
  37. $record["timestamp"] = $record["datetime"]->format("Y-m-d\TH:i:s.uO");
  38. // TODO 2.0 unset the 'datetime' parameter, retained for BC
  39. }
  40. return parent::format($record);
  41. }
  42. }