LogmaticFormatter.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 Logmatic.
  13. *
  14. * @author Julien Breux <julien.breux@gmail.com>
  15. */
  16. class LogmaticFormatter extends JsonFormatter
  17. {
  18. protected const MARKERS = ["sourcecode", "php"];
  19. /**
  20. * @var string
  21. */
  22. protected $hostname = '';
  23. /**
  24. * @var string
  25. */
  26. protected $appname = '';
  27. public function setHostname(string $hostname): self
  28. {
  29. $this->hostname = $hostname;
  30. return $this;
  31. }
  32. public function setAppname(string $appname): self
  33. {
  34. $this->appname = $appname;
  35. return $this;
  36. }
  37. /**
  38. * Appends the 'hostname' and 'appname' parameter for indexing by Logmatic.
  39. *
  40. * @see http://doc.logmatic.io/docs/basics-to-send-data
  41. * @see \Monolog\Formatter\JsonFormatter::format()
  42. */
  43. public function format(array $record): string
  44. {
  45. if (!empty($this->hostname)) {
  46. $record["hostname"] = $this->hostname;
  47. }
  48. if (!empty($this->appname)) {
  49. $record["appname"] = $this->appname;
  50. }
  51. $record["@marker"] = static::MARKERS;
  52. return parent::format($record);
  53. }
  54. }