WildfireFormatter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. use Monolog\Logger;
  12. /**
  13. * Serializes a log message according to Wildfire's header requirements
  14. *
  15. * @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
  16. * @author Christophe Coevoet <stof@notk.org>
  17. * @author Kirill chEbba Chebunin <iam@chebba.org>
  18. */
  19. class WildfireFormatter extends NormalizerFormatter
  20. {
  21. /**
  22. * Translates Monolog log levels to Wildfire levels.
  23. */
  24. private $logLevels = array(
  25. Logger::DEBUG => 'LOG',
  26. Logger::INFO => 'INFO',
  27. Logger::NOTICE => 'INFO',
  28. Logger::WARNING => 'WARN',
  29. Logger::ERROR => 'ERROR',
  30. Logger::CRITICAL => 'ERROR',
  31. Logger::ALERT => 'ERROR',
  32. Logger::EMERGENCY => 'ERROR',
  33. );
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function format(array $record)
  38. {
  39. // Retrieve the line and file if set and remove them from the formatted extra
  40. $file = $line = '';
  41. if (isset($record['extra']['file'])) {
  42. $file = $record['extra']['file'];
  43. unset($record['extra']['file']);
  44. }
  45. if (isset($record['extra']['line'])) {
  46. $line = $record['extra']['line'];
  47. unset($record['extra']['line']);
  48. }
  49. $record = $this->normalize($record);
  50. $message = array('message' => $record['message']);
  51. if ($record['context']) {
  52. $message['context'] = $record['context'];
  53. }
  54. if ($record['extra']) {
  55. $message['extra'] = $record['extra'];
  56. }
  57. if (count($message) === 1) {
  58. $message = reset($message);
  59. }
  60. // Create JSON object describing the appearance of the message in the console
  61. $json = json_encode(array(
  62. array(
  63. 'Type' => $this->logLevels[$record['level']],
  64. 'File' => $file,
  65. 'Line' => $line,
  66. 'Label' => $record['channel'],
  67. ),
  68. $message,
  69. ));
  70. // The message itself is a serialization of the above JSON object + it's length
  71. return sprintf(
  72. '%s|%s|',
  73. strlen($json),
  74. $json
  75. );
  76. }
  77. public function formatBatch(array $records)
  78. {
  79. throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
  80. }
  81. protected function normalize($data)
  82. {
  83. if (is_object($data) && !$data instanceof \DateTime) {
  84. return $data;
  85. }
  86. return parent::normalize($data);
  87. }
  88. }