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