|
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\Handler; |
|
|
13 |
|
|
|
14 |
use Monolog\Logger; |
|
|
15 |
use Monolog\Formatter\WildfireFormatter; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Simple FirePHP Handler (http://www.firephp.org/), which uses the Wildfire protocol. |
|
|
19 |
* |
|
|
20 |
* @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com> |
|
|
21 |
*/ |
|
|
22 |
class FirePHPHandler extends AbstractProcessingHandler |
|
|
23 |
{ |
|
|
24 |
/** |
|
|
25 |
* WildFire JSON header message format |
|
|
26 |
*/ |
|
|
27 |
const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2'; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* FirePHP structure for parsing messages & their presentation |
|
|
31 |
*/ |
|
|
32 |
const STRUCTURE_URI = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'; |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* Must reference a "known" plugin, otherwise headers won't display in FirePHP |
|
|
36 |
*/ |
|
|
37 |
const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3'; |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Header prefix for Wildfire to recognize & parse headers |
|
|
41 |
*/ |
|
|
42 |
const HEADER_PREFIX = 'X-Wf'; |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* Whether or not Wildfire vendor-specific headers have been generated & sent yet |
|
|
46 |
*/ |
|
|
47 |
protected static $initialized = false; |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Shared static message index between potentially multiple handlers |
|
|
51 |
* @var int |
|
|
52 |
*/ |
|
|
53 |
protected static $messageIndex = 1; |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Base header creation function used by init headers & record headers |
|
|
57 |
* |
|
|
58 |
* @param array $meta Wildfire Plugin, Protocol & Structure Indexes |
|
|
59 |
* @param string $message Log message |
|
|
60 |
* @return array Complete header string ready for the client as key and message as value |
|
|
61 |
*/ |
|
|
62 |
protected function createHeader(array $meta, $message) |
|
|
63 |
{ |
|
|
64 |
$header = sprintf('%s-%s', self::HEADER_PREFIX, join('-', $meta)); |
|
|
65 |
|
|
|
66 |
return array($header => $message); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
/** |
|
|
70 |
* Creates message header from record |
|
|
71 |
* |
|
|
72 |
* @see createHeader() |
|
|
73 |
* @param array $record |
|
|
74 |
* @return string |
|
|
75 |
*/ |
|
|
76 |
protected function createRecordHeader(array $record) |
|
|
77 |
{ |
|
|
78 |
// Wildfire is extensible to support multiple protocols & plugins in a single request, |
|
|
79 |
// but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake. |
|
|
80 |
return $this->createHeader( |
|
|
81 |
array(1, 1, 1, self::$messageIndex++), |
|
|
82 |
$record['formatted'] |
|
|
83 |
); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
/** |
|
|
87 |
* {@inheritDoc} |
|
|
88 |
*/ |
|
|
89 |
protected function getDefaultFormatter() |
|
|
90 |
{ |
|
|
91 |
return new WildfireFormatter(); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
/** |
|
|
95 |
* Wildfire initialization headers to enable message parsing |
|
|
96 |
* |
|
|
97 |
* @see createHeader() |
|
|
98 |
* @see sendHeader() |
|
|
99 |
* @return array |
|
|
100 |
*/ |
|
|
101 |
protected function getInitHeaders() |
|
|
102 |
{ |
|
|
103 |
// Initial payload consists of required headers for Wildfire |
|
|
104 |
return array_merge( |
|
|
105 |
$this->createHeader(array('Protocol', 1), self::PROTOCOL_URI), |
|
|
106 |
$this->createHeader(array(1, 'Structure', 1), self::STRUCTURE_URI), |
|
|
107 |
$this->createHeader(array(1, 'Plugin', 1), self::PLUGIN_URI) |
|
|
108 |
); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
/** |
|
|
112 |
* Send header string to the client |
|
|
113 |
* |
|
|
114 |
* @param string $header |
|
|
115 |
* @param string $content |
|
|
116 |
*/ |
|
|
117 |
protected function sendHeader($header, $content) |
|
|
118 |
{ |
|
|
119 |
if (!headers_sent()) { |
|
|
120 |
header(sprintf('%s: %s', $header, $content)); |
|
|
121 |
} |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* Creates & sends header for a record, ensuring init headers have been sent prior |
|
|
126 |
* |
|
|
127 |
* @see sendHeader() |
|
|
128 |
* @see sendInitHeaders() |
|
|
129 |
* @param array $record |
|
|
130 |
*/ |
|
|
131 |
protected function write(array $record) |
|
|
132 |
{ |
|
|
133 |
// WildFire-specific headers must be sent prior to any messages |
|
|
134 |
if (!self::$initialized) { |
|
|
135 |
foreach ($this->getInitHeaders() as $header => $content) { |
|
|
136 |
$this->sendHeader($header, $content); |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
self::$initialized = true; |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
$header = $this->createRecordHeader($record); |
|
|
143 |
$this->sendHeader(key($header), current($header)); |
|
|
144 |
} |
|
|
145 |
} |