PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/monolog/monolog/tests/Monolog/Formatter/LogstashFormatterTest.php

https://gitlab.com/ealexis.t/trends
PHP | 333 lines | 238 code | 56 blank | 39 comment | 3 complexity | d5e5c2b1fc656c682c35118e60fef41a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. use Monolog\Logger;
  12. class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function tearDown()
  15. {
  16. \PHPUnit_Framework_Error_Warning::$enabled = true;
  17. return parent::tearDown();
  18. }
  19. /**
  20. * @covers Monolog\Formatter\LogstashFormatter::format
  21. */
  22. public function testDefaultFormatter()
  23. {
  24. $formatter = new LogstashFormatter('test', 'hostname');
  25. $record = array(
  26. 'level' => Logger::ERROR,
  27. 'level_name' => 'ERROR',
  28. 'channel' => 'meh',
  29. 'context' => array(),
  30. 'datetime' => new \DateTime("@0"),
  31. 'extra' => array(),
  32. 'message' => 'log',
  33. );
  34. $message = json_decode($formatter->format($record), true);
  35. $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
  36. $this->assertEquals('log', $message['@message']);
  37. $this->assertEquals('meh', $message['@fields']['channel']);
  38. $this->assertContains('meh', $message['@tags']);
  39. $this->assertEquals(Logger::ERROR, $message['@fields']['level']);
  40. $this->assertEquals('test', $message['@type']);
  41. $this->assertEquals('hostname', $message['@source']);
  42. $formatter = new LogstashFormatter('mysystem');
  43. $message = json_decode($formatter->format($record), true);
  44. $this->assertEquals('mysystem', $message['@type']);
  45. }
  46. /**
  47. * @covers Monolog\Formatter\LogstashFormatter::format
  48. */
  49. public function testFormatWithFileAndLine()
  50. {
  51. $formatter = new LogstashFormatter('test');
  52. $record = array(
  53. 'level' => Logger::ERROR,
  54. 'level_name' => 'ERROR',
  55. 'channel' => 'meh',
  56. 'context' => array('from' => 'logger'),
  57. 'datetime' => new \DateTime("@0"),
  58. 'extra' => array('file' => 'test', 'line' => 14),
  59. 'message' => 'log',
  60. );
  61. $message = json_decode($formatter->format($record), true);
  62. $this->assertEquals('test', $message['@fields']['file']);
  63. $this->assertEquals(14, $message['@fields']['line']);
  64. }
  65. /**
  66. * @covers Monolog\Formatter\LogstashFormatter::format
  67. */
  68. public function testFormatWithContext()
  69. {
  70. $formatter = new LogstashFormatter('test');
  71. $record = array(
  72. 'level' => Logger::ERROR,
  73. 'level_name' => 'ERROR',
  74. 'channel' => 'meh',
  75. 'context' => array('from' => 'logger'),
  76. 'datetime' => new \DateTime("@0"),
  77. 'extra' => array('key' => 'pair'),
  78. 'message' => 'log',
  79. );
  80. $message = json_decode($formatter->format($record), true);
  81. $message_array = $message['@fields'];
  82. $this->assertArrayHasKey('ctxt_from', $message_array);
  83. $this->assertEquals('logger', $message_array['ctxt_from']);
  84. // Test with extraPrefix
  85. $formatter = new LogstashFormatter('test', null, null, 'CTX');
  86. $message = json_decode($formatter->format($record), true);
  87. $message_array = $message['@fields'];
  88. $this->assertArrayHasKey('CTXfrom', $message_array);
  89. $this->assertEquals('logger', $message_array['CTXfrom']);
  90. }
  91. /**
  92. * @covers Monolog\Formatter\LogstashFormatter::format
  93. */
  94. public function testFormatWithExtra()
  95. {
  96. $formatter = new LogstashFormatter('test');
  97. $record = array(
  98. 'level' => Logger::ERROR,
  99. 'level_name' => 'ERROR',
  100. 'channel' => 'meh',
  101. 'context' => array('from' => 'logger'),
  102. 'datetime' => new \DateTime("@0"),
  103. 'extra' => array('key' => 'pair'),
  104. 'message' => 'log',
  105. );
  106. $message = json_decode($formatter->format($record), true);
  107. $message_array = $message['@fields'];
  108. $this->assertArrayHasKey('key', $message_array);
  109. $this->assertEquals('pair', $message_array['key']);
  110. // Test with extraPrefix
  111. $formatter = new LogstashFormatter('test', null, 'EXT');
  112. $message = json_decode($formatter->format($record), true);
  113. $message_array = $message['@fields'];
  114. $this->assertArrayHasKey('EXTkey', $message_array);
  115. $this->assertEquals('pair', $message_array['EXTkey']);
  116. }
  117. public function testFormatWithApplicationName()
  118. {
  119. $formatter = new LogstashFormatter('app', 'test');
  120. $record = array(
  121. 'level' => Logger::ERROR,
  122. 'level_name' => 'ERROR',
  123. 'channel' => 'meh',
  124. 'context' => array('from' => 'logger'),
  125. 'datetime' => new \DateTime("@0"),
  126. 'extra' => array('key' => 'pair'),
  127. 'message' => 'log',
  128. );
  129. $message = json_decode($formatter->format($record), true);
  130. $this->assertArrayHasKey('@type', $message);
  131. $this->assertEquals('app', $message['@type']);
  132. }
  133. /**
  134. * @covers Monolog\Formatter\LogstashFormatter::format
  135. */
  136. public function testDefaultFormatterV1()
  137. {
  138. $formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1);
  139. $record = array(
  140. 'level' => Logger::ERROR,
  141. 'level_name' => 'ERROR',
  142. 'channel' => 'meh',
  143. 'context' => array(),
  144. 'datetime' => new \DateTime("@0"),
  145. 'extra' => array(),
  146. 'message' => 'log',
  147. );
  148. $message = json_decode($formatter->format($record), true);
  149. $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
  150. $this->assertEquals("1", $message['@version']);
  151. $this->assertEquals('log', $message['message']);
  152. $this->assertEquals('meh', $message['channel']);
  153. $this->assertEquals('ERROR', $message['level']);
  154. $this->assertEquals('test', $message['type']);
  155. $this->assertEquals('hostname', $message['host']);
  156. $formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1);
  157. $message = json_decode($formatter->format($record), true);
  158. $this->assertEquals('mysystem', $message['type']);
  159. }
  160. /**
  161. * @covers Monolog\Formatter\LogstashFormatter::format
  162. */
  163. public function testFormatWithFileAndLineV1()
  164. {
  165. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  166. $record = array(
  167. 'level' => Logger::ERROR,
  168. 'level_name' => 'ERROR',
  169. 'channel' => 'meh',
  170. 'context' => array('from' => 'logger'),
  171. 'datetime' => new \DateTime("@0"),
  172. 'extra' => array('file' => 'test', 'line' => 14),
  173. 'message' => 'log',
  174. );
  175. $message = json_decode($formatter->format($record), true);
  176. $this->assertEquals('test', $message['file']);
  177. $this->assertEquals(14, $message['line']);
  178. }
  179. /**
  180. * @covers Monolog\Formatter\LogstashFormatter::format
  181. */
  182. public function testFormatWithContextV1()
  183. {
  184. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  185. $record = array(
  186. 'level' => Logger::ERROR,
  187. 'level_name' => 'ERROR',
  188. 'channel' => 'meh',
  189. 'context' => array('from' => 'logger'),
  190. 'datetime' => new \DateTime("@0"),
  191. 'extra' => array('key' => 'pair'),
  192. 'message' => 'log',
  193. );
  194. $message = json_decode($formatter->format($record), true);
  195. $this->assertArrayHasKey('ctxt_from', $message);
  196. $this->assertEquals('logger', $message['ctxt_from']);
  197. // Test with extraPrefix
  198. $formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1);
  199. $message = json_decode($formatter->format($record), true);
  200. $this->assertArrayHasKey('CTXfrom', $message);
  201. $this->assertEquals('logger', $message['CTXfrom']);
  202. }
  203. /**
  204. * @covers Monolog\Formatter\LogstashFormatter::format
  205. */
  206. public function testFormatWithExtraV1()
  207. {
  208. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  209. $record = array(
  210. 'level' => Logger::ERROR,
  211. 'level_name' => 'ERROR',
  212. 'channel' => 'meh',
  213. 'context' => array('from' => 'logger'),
  214. 'datetime' => new \DateTime("@0"),
  215. 'extra' => array('key' => 'pair'),
  216. 'message' => 'log',
  217. );
  218. $message = json_decode($formatter->format($record), true);
  219. $this->assertArrayHasKey('key', $message);
  220. $this->assertEquals('pair', $message['key']);
  221. // Test with extraPrefix
  222. $formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1);
  223. $message = json_decode($formatter->format($record), true);
  224. $this->assertArrayHasKey('EXTkey', $message);
  225. $this->assertEquals('pair', $message['EXTkey']);
  226. }
  227. public function testFormatWithApplicationNameV1()
  228. {
  229. $formatter = new LogstashFormatter('app', 'test', null, 'ctxt_', LogstashFormatter::V1);
  230. $record = array(
  231. 'level' => Logger::ERROR,
  232. 'level_name' => 'ERROR',
  233. 'channel' => 'meh',
  234. 'context' => array('from' => 'logger'),
  235. 'datetime' => new \DateTime("@0"),
  236. 'extra' => array('key' => 'pair'),
  237. 'message' => 'log',
  238. );
  239. $message = json_decode($formatter->format($record), true);
  240. $this->assertArrayHasKey('type', $message);
  241. $this->assertEquals('app', $message['type']);
  242. }
  243. public function testFormatWithLatin9Data()
  244. {
  245. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  246. // Ignore the warning that will be emitted by PHP <5.5.0
  247. \PHPUnit_Framework_Error_Warning::$enabled = false;
  248. }
  249. $formatter = new LogstashFormatter('test', 'hostname');
  250. $record = array(
  251. 'level' => Logger::ERROR,
  252. 'level_name' => 'ERROR',
  253. 'channel' => '¯\_(ツ)_/¯',
  254. 'context' => array(),
  255. 'datetime' => new \DateTime("@0"),
  256. 'extra' => array(
  257. 'user_agent' => "\xD6WN; FBCR/OrangeEspa\xF1a; Vers\xE3o/4.0; F\xE4rist",
  258. ),
  259. 'message' => 'log',
  260. );
  261. $message = json_decode($formatter->format($record), true);
  262. $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
  263. $this->assertEquals('log', $message['@message']);
  264. $this->assertEquals('¯\_(ツ)_/¯', $message['@fields']['channel']);
  265. $this->assertContains('¯\_(ツ)_/¯', $message['@tags']);
  266. $this->assertEquals(Logger::ERROR, $message['@fields']['level']);
  267. $this->assertEquals('test', $message['@type']);
  268. $this->assertEquals('hostname', $message['@source']);
  269. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  270. $this->assertEquals('ÖWN; FBCR/OrangeEspaña; Versão/4.0; Färist', $message['@fields']['user_agent']);
  271. } else {
  272. // PHP <5.5 does not return false for an element encoding failure,
  273. // instead it emits a warning (possibly) and nulls the value.
  274. $this->assertEquals(null, $message['@fields']['user_agent']);
  275. }
  276. }
  277. }