ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
LogstashFormatterTest.php
Go to the documentation of this file.
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 
17 {
18  public function tearDown()
19  {
20  \PHPUnit_Framework_Error_Warning::$enabled = true;
21 
22  return parent::tearDown();
23  }
24 
28  public function testDefaultFormatter()
29  {
30  $formatter = new LogstashFormatter('test', 'hostname');
31  $record = array(
32  'level' => Logger::ERROR,
33  'level_name' => 'ERROR',
34  'channel' => 'meh',
35  'context' => array(),
36  'datetime' => new \DateTime("@0"),
37  'extra' => array(),
38  'message' => 'log',
39  );
40 
41  $message = json_decode($formatter->format($record), true);
42 
43  $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
44  $this->assertEquals('log', $message['@message']);
45  $this->assertEquals('meh', $message['@fields']['channel']);
46  $this->assertContains('meh', $message['@tags']);
47  $this->assertEquals(Logger::ERROR, $message['@fields']['level']);
48  $this->assertEquals('test', $message['@type']);
49  $this->assertEquals('hostname', $message['@source']);
50 
51  $formatter = new LogstashFormatter('mysystem');
52 
53  $message = json_decode($formatter->format($record), true);
54 
55  $this->assertEquals('mysystem', $message['@type']);
56  }
57 
61  public function testFormatWithFileAndLine()
62  {
63  $formatter = new LogstashFormatter('test');
64  $record = array(
65  'level' => Logger::ERROR,
66  'level_name' => 'ERROR',
67  'channel' => 'meh',
68  'context' => array('from' => 'logger'),
69  'datetime' => new \DateTime("@0"),
70  'extra' => array('file' => 'test', 'line' => 14),
71  'message' => 'log',
72  );
73 
74  $message = json_decode($formatter->format($record), true);
75 
76  $this->assertEquals('test', $message['@fields']['file']);
77  $this->assertEquals(14, $message['@fields']['line']);
78  }
79 
83  public function testFormatWithContext()
84  {
85  $formatter = new LogstashFormatter('test');
86  $record = array(
87  'level' => Logger::ERROR,
88  'level_name' => 'ERROR',
89  'channel' => 'meh',
90  'context' => array('from' => 'logger'),
91  'datetime' => new \DateTime("@0"),
92  'extra' => array('key' => 'pair'),
93  'message' => 'log',
94  );
95 
96  $message = json_decode($formatter->format($record), true);
97 
98  $message_array = $message['@fields'];
99 
100  $this->assertArrayHasKey('ctxt_from', $message_array);
101  $this->assertEquals('logger', $message_array['ctxt_from']);
102 
103  // Test with extraPrefix
104  $formatter = new LogstashFormatter('test', null, null, 'CTX');
105  $message = json_decode($formatter->format($record), true);
106 
107  $message_array = $message['@fields'];
108 
109  $this->assertArrayHasKey('CTXfrom', $message_array);
110  $this->assertEquals('logger', $message_array['CTXfrom']);
111  }
112 
116  public function testFormatWithExtra()
117  {
118  $formatter = new LogstashFormatter('test');
119  $record = array(
120  'level' => Logger::ERROR,
121  'level_name' => 'ERROR',
122  'channel' => 'meh',
123  'context' => array('from' => 'logger'),
124  'datetime' => new \DateTime("@0"),
125  'extra' => array('key' => 'pair'),
126  'message' => 'log',
127  );
128 
129  $message = json_decode($formatter->format($record), true);
130 
131  $message_array = $message['@fields'];
132 
133  $this->assertArrayHasKey('key', $message_array);
134  $this->assertEquals('pair', $message_array['key']);
135 
136  // Test with extraPrefix
137  $formatter = new LogstashFormatter('test', null, 'EXT');
138  $message = json_decode($formatter->format($record), true);
139 
140  $message_array = $message['@fields'];
141 
142  $this->assertArrayHasKey('EXTkey', $message_array);
143  $this->assertEquals('pair', $message_array['EXTkey']);
144  }
145 
147  {
148  $formatter = new LogstashFormatter('app', 'test');
149  $record = array(
150  'level' => Logger::ERROR,
151  'level_name' => 'ERROR',
152  'channel' => 'meh',
153  'context' => array('from' => 'logger'),
154  'datetime' => new \DateTime("@0"),
155  'extra' => array('key' => 'pair'),
156  'message' => 'log',
157  );
158 
159  $message = json_decode($formatter->format($record), true);
160 
161  $this->assertArrayHasKey('@type', $message);
162  $this->assertEquals('app', $message['@type']);
163  }
164 
168  public function testDefaultFormatterV1()
169  {
170  $formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1);
171  $record = array(
172  'level' => Logger::ERROR,
173  'level_name' => 'ERROR',
174  'channel' => 'meh',
175  'context' => array(),
176  'datetime' => new \DateTime("@0"),
177  'extra' => array(),
178  'message' => 'log',
179  );
180 
181  $message = json_decode($formatter->format($record), true);
182 
183  $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
184  $this->assertEquals("1", $message['@version']);
185  $this->assertEquals('log', $message['message']);
186  $this->assertEquals('meh', $message['channel']);
187  $this->assertEquals('ERROR', $message['level']);
188  $this->assertEquals('test', $message['type']);
189  $this->assertEquals('hostname', $message['host']);
190 
191  $formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1);
192 
193  $message = json_decode($formatter->format($record), true);
194 
195  $this->assertEquals('mysystem', $message['type']);
196  }
197 
201  public function testFormatWithFileAndLineV1()
202  {
203  $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
204  $record = array(
205  'level' => Logger::ERROR,
206  'level_name' => 'ERROR',
207  'channel' => 'meh',
208  'context' => array('from' => 'logger'),
209  'datetime' => new \DateTime("@0"),
210  'extra' => array('file' => 'test', 'line' => 14),
211  'message' => 'log',
212  );
213 
214  $message = json_decode($formatter->format($record), true);
215 
216  $this->assertEquals('test', $message['file']);
217  $this->assertEquals(14, $message['line']);
218  }
219 
223  public function testFormatWithContextV1()
224  {
225  $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
226  $record = array(
227  'level' => Logger::ERROR,
228  'level_name' => 'ERROR',
229  'channel' => 'meh',
230  'context' => array('from' => 'logger'),
231  'datetime' => new \DateTime("@0"),
232  'extra' => array('key' => 'pair'),
233  'message' => 'log',
234  );
235 
236  $message = json_decode($formatter->format($record), true);
237 
238  $this->assertArrayHasKey('ctxt_from', $message);
239  $this->assertEquals('logger', $message['ctxt_from']);
240 
241  // Test with extraPrefix
242  $formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1);
243  $message = json_decode($formatter->format($record), true);
244 
245  $this->assertArrayHasKey('CTXfrom', $message);
246  $this->assertEquals('logger', $message['CTXfrom']);
247  }
248 
252  public function testFormatWithExtraV1()
253  {
254  $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
255  $record = array(
256  'level' => Logger::ERROR,
257  'level_name' => 'ERROR',
258  'channel' => 'meh',
259  'context' => array('from' => 'logger'),
260  'datetime' => new \DateTime("@0"),
261  'extra' => array('key' => 'pair'),
262  'message' => 'log',
263  );
264 
265  $message = json_decode($formatter->format($record), true);
266 
267  $this->assertArrayHasKey('key', $message);
268  $this->assertEquals('pair', $message['key']);
269 
270  // Test with extraPrefix
271  $formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1);
272  $message = json_decode($formatter->format($record), true);
273 
274  $this->assertArrayHasKey('EXTkey', $message);
275  $this->assertEquals('pair', $message['EXTkey']);
276  }
277 
279  {
280  $formatter = new LogstashFormatter('app', 'test', null, 'ctxt_', LogstashFormatter::V1);
281  $record = array(
282  'level' => Logger::ERROR,
283  'level_name' => 'ERROR',
284  'channel' => 'meh',
285  'context' => array('from' => 'logger'),
286  'datetime' => new \DateTime("@0"),
287  'extra' => array('key' => 'pair'),
288  'message' => 'log',
289  );
290 
291  $message = json_decode($formatter->format($record), true);
292 
293  $this->assertArrayHasKey('type', $message);
294  $this->assertEquals('app', $message['type']);
295  }
296 
297  public function testFormatWithLatin9Data()
298  {
299  if (version_compare(PHP_VERSION, '5.5.0', '<')) {
300  // Ignore the warning that will be emitted by PHP <5.5.0
301  \PHPUnit_Framework_Error_Warning::$enabled = false;
302  }
303  $formatter = new LogstashFormatter('test', 'hostname');
304  $record = array(
305  'level' => Logger::ERROR,
306  'level_name' => 'ERROR',
307  'channel' => '¯\_(ツ)_/¯',
308  'context' => array(),
309  'datetime' => new \DateTime("@0"),
310  'extra' => array(
311  'user_agent' => "\xD6WN; FBCR/OrangeEspa\xF1a; Vers\xE3o/4.0; F\xE4rist",
312  ),
313  'message' => 'log',
314  );
315 
316  $message = json_decode($formatter->format($record), true);
317 
318  $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
319  $this->assertEquals('log', $message['@message']);
320  $this->assertEquals('¯\_(ツ)_/¯', $message['@fields']['channel']);
321  $this->assertContains('¯\_(ツ)_/¯', $message['@tags']);
322  $this->assertEquals(Logger::ERROR, $message['@fields']['level']);
323  $this->assertEquals('test', $message['@type']);
324  $this->assertEquals('hostname', $message['@source']);
325  if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
326  $this->assertEquals('ÖWN; FBCR/OrangeEspaña; Versão/4.0; Färist', $message['@fields']['user_agent']);
327  } else {
328  // PHP <5.5 does not return false for an element encoding failure,
329  // instead it emits a warning (possibly) and nulls the value.
330  $this->assertEquals(null, $message['@fields']['user_agent']);
331  }
332  }
333 }
const ERROR
Runtime errors.
Definition: Logger.php:57
Create styles array
The data for the language used.
Serializes a log message to Logstash Event Format.