LogmaticFormatter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 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)
  28. {
  29. $this->hostname = $hostname;
  30. }
  31. public function setAppname(string $appname)
  32. {
  33. $this->appname = $appname;
  34. }
  35. /**
  36. * Appends the 'hostname' and 'appname' parameter for indexing by Logmatic.
  37. *
  38. * @see http://doc.logmatic.io/docs/basics-to-send-data
  39. * @see \Monolog\Formatter\JsonFormatter::format()
  40. */
  41. public function format(array $record): string
  42. {
  43. if (!empty($this->hostname)) {
  44. $record["hostname"] = $this->hostname;
  45. }
  46. if (!empty($this->appname)) {
  47. $record["appname"] = $this->appname;
  48. }
  49. $record["@marker"] = self::MARKERS;
  50. return parent::format($record);
  51. }
  52. }