ChromePHPFormatter.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. use Monolog\Logger;
  12. /**
  13. * Formats a log message according to the ChromePHP array format
  14. *
  15. * @author Christophe Coevoet <stof@notk.org>
  16. */
  17. class ChromePHPFormatter implements FormatterInterface
  18. {
  19. /**
  20. * Translates Monolog log levels to Wildfire levels.
  21. */
  22. private $logLevels = [
  23. Logger::DEBUG => 'log',
  24. Logger::INFO => 'info',
  25. Logger::NOTICE => 'info',
  26. Logger::WARNING => 'warn',
  27. Logger::ERROR => 'error',
  28. Logger::CRITICAL => 'error',
  29. Logger::ALERT => 'error',
  30. Logger::EMERGENCY => '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. $backtrace = 'unknown';
  39. if (isset($record['extra']['file'], $record['extra']['line'])) {
  40. $backtrace = $record['extra']['file'].' : '.$record['extra']['line'];
  41. unset($record['extra']['file'], $record['extra']['line']);
  42. }
  43. $message = ['message' => $record['message']];
  44. if ($record['context']) {
  45. $message['context'] = $record['context'];
  46. }
  47. if ($record['extra']) {
  48. $message['extra'] = $record['extra'];
  49. }
  50. if (count($message) === 1) {
  51. $message = reset($message);
  52. }
  53. return [
  54. $record['channel'],
  55. $message,
  56. $backtrace,
  57. $this->logLevels[$record['level']],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function formatBatch(array $records)
  64. {
  65. $formatted = [];
  66. foreach ($records as $record) {
  67. $formatted[] = $this->format($record);
  68. }
  69. return $formatted;
  70. }
  71. }