WildfireFormatter.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 implements FormatterInterface
  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::WARNING => 'WARN',
  28. Logger::ERROR => 'ERROR',
  29. Logger::CRITICAL => 'ERROR',
  30. Logger::ALERT => 'ERROR',
  31. );
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function format(array $record)
  36. {
  37. // Retrieve the line and file if set and remove them from the formatted extra
  38. $file = $line = '';
  39. if (isset($record['extra']['file'])) {
  40. $file = $record['extra']['file'];
  41. unset($record['extra']['file']);
  42. }
  43. if (isset($record['extra']['line'])) {
  44. $line = $record['extra']['line'];
  45. unset($record['extra']['line']);
  46. }
  47. $message = array('message' => $record['message']);
  48. if ($record['context']) {
  49. $message['context'] = $record['context'];
  50. }
  51. if ($record['extra']) {
  52. $message['extra'] = $record['extra'];
  53. }
  54. if (count($message) === 1) {
  55. $message = reset($message);
  56. }
  57. // Create JSON object describing the appearance of the message in the console
  58. $json = json_encode(array(
  59. array(
  60. 'Type' => $this->logLevels[$record['level']],
  61. 'File' => $file,
  62. 'Line' => $line,
  63. 'Label' => $record['channel'],
  64. ),
  65. $message,
  66. ));
  67. // The message itself is a serialization of the above JSON object + it's length
  68. return sprintf(
  69. '%s|%s|',
  70. strlen($json),
  71. $json
  72. );
  73. }
  74. public function formatBatch(array $records)
  75. {
  76. throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
  77. }
  78. }