1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog\Formatter; 13 14use Monolog\Logger; 15 16/** 17 * Formats a log message according to the ChromePHP array format 18 * 19 * @author Christophe Coevoet <stof@notk.org> 20 */ 21class ChromePHPFormatter implements FormatterInterface 22{ 23 /** 24 * Translates Monolog log levels to Wildfire levels. 25 * 26 * @var array<int, 'log'|'info'|'warn'|'error'> 27 */ 28 private $logLevels = [ 29 Logger::DEBUG => 'log', 30 Logger::INFO => 'info', 31 Logger::NOTICE => 'info', 32 Logger::WARNING => 'warn', 33 Logger::ERROR => 'error', 34 Logger::CRITICAL => 'error', 35 Logger::ALERT => 'error', 36 Logger::EMERGENCY => 'error', 37 ]; 38 39 /** 40 * {@inheritDoc} 41 */ 42 public function format(array $record) 43 { 44 // Retrieve the line and file if set and remove them from the formatted extra 45 $backtrace = 'unknown'; 46 if (isset($record['extra']['file'], $record['extra']['line'])) { 47 $backtrace = $record['extra']['file'].' : '.$record['extra']['line']; 48 unset($record['extra']['file'], $record['extra']['line']); 49 } 50 51 $message = ['message' => $record['message']]; 52 if ($record['context']) { 53 $message['context'] = $record['context']; 54 } 55 if ($record['extra']) { 56 $message['extra'] = $record['extra']; 57 } 58 if (count($message) === 1) { 59 $message = reset($message); 60 } 61 62 return [ 63 $record['channel'], 64 $message, 65 $backtrace, 66 $this->logLevels[$record['level']], 67 ]; 68 } 69 70 /** 71 * {@inheritDoc} 72 */ 73 public function formatBatch(array $records) 74 { 75 $formatted = []; 76 77 foreach ($records as $record) { 78 $formatted[] = $this->format($record); 79 } 80 81 return $formatted; 82 } 83} 84