PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/monolog/monolog/src/Monolog/Formatter/WildfireFormatter.php

https://gitlab.com/techniconline/kmc
PHP | 113 lines | 76 code | 13 blank | 24 comment | 9 complexity | 67c6612cf2c752039c21deb5ef5a3e37 MD5 | raw file
  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. const TABLE = 'table';
  22. /**
  23. * Translates Monolog log levels to Wildfire levels.
  24. */
  25. private $logLevels = array(
  26. Logger::DEBUG => 'LOG',
  27. Logger::INFO => 'INFO',
  28. Logger::NOTICE => 'INFO',
  29. Logger::WARNING => 'WARN',
  30. Logger::ERROR => 'ERROR',
  31. Logger::CRITICAL => 'ERROR',
  32. Logger::ALERT => 'ERROR',
  33. Logger::EMERGENCY => 'ERROR',
  34. );
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function format(array $record)
  39. {
  40. // Retrieve the line and file if set and remove them from the formatted extra
  41. $file = $line = '';
  42. if (isset($record['extra']['file'])) {
  43. $file = $record['extra']['file'];
  44. unset($record['extra']['file']);
  45. }
  46. if (isset($record['extra']['line'])) {
  47. $line = $record['extra']['line'];
  48. unset($record['extra']['line']);
  49. }
  50. $record = $this->normalize($record);
  51. $message = array('message' => $record['message']);
  52. $handleError = false;
  53. if ($record['context']) {
  54. $message['context'] = $record['context'];
  55. $handleError = true;
  56. }
  57. if ($record['extra']) {
  58. $message['extra'] = $record['extra'];
  59. $handleError = true;
  60. }
  61. if (count($message) === 1) {
  62. $message = reset($message);
  63. }
  64. if (isset($record['context'][self::TABLE])) {
  65. $type = 'TABLE';
  66. $label = $record['channel'] . ': ' . $record['message'];
  67. $message = $record['context'][self::TABLE];
  68. } else {
  69. $type = $this->logLevels[$record['level']];
  70. $label = $record['channel'];
  71. }
  72. // Create JSON object describing the appearance of the message in the console
  73. $json = $this->toJson(array(
  74. array(
  75. 'Type' => $type,
  76. 'File' => $file,
  77. 'Line' => $line,
  78. 'Label' => $label,
  79. ),
  80. $message,
  81. ), $handleError);
  82. // The message itself is a serialization of the above JSON object + it's length
  83. return sprintf(
  84. '%s|%s|',
  85. strlen($json),
  86. $json
  87. );
  88. }
  89. public function formatBatch(array $records)
  90. {
  91. throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
  92. }
  93. protected function normalize($data)
  94. {
  95. if (is_object($data) && !$data instanceof \DateTime) {
  96. return $data;
  97. }
  98. return parent::normalize($data);
  99. }
  100. }