scratch – Blame information for rev 115

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