LogstashFormatterTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. use Monolog\Formatter\LogstashFormatter;
  13. class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\LogstashFormatter::format
  17. */
  18. public function testDefaultFormatter()
  19. {
  20. $formatter = new LogstashFormatter('test', 'hostname');
  21. $record = array(
  22. 'level' => Logger::ERROR,
  23. 'level_name' => 'ERROR',
  24. 'channel' => 'meh',
  25. 'context' => array(),
  26. 'datetime' => new \DateTime("@0"),
  27. 'extra' => array(),
  28. 'message' => 'log',
  29. );
  30. $message = json_decode($formatter->format($record), true);
  31. $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
  32. $this->assertEquals('log', $message['@message']);
  33. $this->assertEquals('meh', $message['@fields']['channel']);
  34. $this->assertContains('meh', $message['@tags']);
  35. $this->assertEquals(Logger::ERROR, $message['@fields']['level']);
  36. $this->assertEquals('test', $message['@type']);
  37. $this->assertEquals('hostname', $message['@source']);
  38. $formatter = new LogstashFormatter('mysystem');
  39. $message = json_decode($formatter->format($record), true);
  40. $this->assertEquals('mysystem', $message['@type']);
  41. }
  42. /**
  43. * @covers Monolog\Formatter\LogstashFormatter::format
  44. */
  45. public function testFormatWithFileAndLine()
  46. {
  47. $formatter = new LogstashFormatter('test');
  48. $record = array(
  49. 'level' => Logger::ERROR,
  50. 'level_name' => 'ERROR',
  51. 'channel' => 'meh',
  52. 'context' => array('from' => 'logger'),
  53. 'datetime' => new \DateTime("@0"),
  54. 'extra' => array('file' => 'test', 'line' => 14),
  55. 'message' => 'log',
  56. );
  57. $message = json_decode($formatter->format($record), true);
  58. $this->assertEquals('test', $message['@fields']['file']);
  59. $this->assertEquals(14, $message['@fields']['line']);
  60. }
  61. /**
  62. * @covers Monolog\Formatter\LogstashFormatter::format
  63. */
  64. public function testFormatWithContext()
  65. {
  66. $formatter = new LogstashFormatter('test');
  67. $record = array(
  68. 'level' => Logger::ERROR,
  69. 'level_name' => 'ERROR',
  70. 'channel' => 'meh',
  71. 'context' => array('from' => 'logger'),
  72. 'datetime' => new \DateTime("@0"),
  73. 'extra' => array('key' => 'pair'),
  74. 'message' => 'log'
  75. );
  76. $message = json_decode($formatter->format($record), true);
  77. $message_array = $message['@fields'];
  78. $this->assertArrayHasKey('ctxt_from', $message_array);
  79. $this->assertEquals('logger', $message_array['ctxt_from']);
  80. // Test with extraPrefix
  81. $formatter = new LogstashFormatter('test', null, null, 'CTX');
  82. $message = json_decode($formatter->format($record), true);
  83. $message_array = $message['@fields'];
  84. $this->assertArrayHasKey('CTXfrom', $message_array);
  85. $this->assertEquals('logger', $message_array['CTXfrom']);
  86. }
  87. /**
  88. * @covers Monolog\Formatter\LogstashFormatter::format
  89. */
  90. public function testFormatWithExtra()
  91. {
  92. $formatter = new LogstashFormatter('test');
  93. $record = array(
  94. 'level' => Logger::ERROR,
  95. 'level_name' => 'ERROR',
  96. 'channel' => 'meh',
  97. 'context' => array('from' => 'logger'),
  98. 'datetime' => new \DateTime("@0"),
  99. 'extra' => array('key' => 'pair'),
  100. 'message' => 'log'
  101. );
  102. $message = json_decode($formatter->format($record), true);
  103. $message_array = $message['@fields'];
  104. $this->assertArrayHasKey('key', $message_array);
  105. $this->assertEquals('pair', $message_array['key']);
  106. // Test with extraPrefix
  107. $formatter = new LogstashFormatter('test', null, 'EXT');
  108. $message = json_decode($formatter->format($record), true);
  109. $message_array = $message['@fields'];
  110. $this->assertArrayHasKey('EXTkey', $message_array);
  111. $this->assertEquals('pair', $message_array['EXTkey']);
  112. }
  113. public function testFormatWithApplicationName()
  114. {
  115. $formatter = new LogstashFormatter('app', 'test');
  116. $record = array(
  117. 'level' => Logger::ERROR,
  118. 'level_name' => 'ERROR',
  119. 'channel' => 'meh',
  120. 'context' => array('from' => 'logger'),
  121. 'datetime' => new \DateTime("@0"),
  122. 'extra' => array('key' => 'pair'),
  123. 'message' => 'log'
  124. );
  125. $message = json_decode($formatter->format($record), true);
  126. $this->assertArrayHasKey('@type', $message);
  127. $this->assertEquals('app', $message['@type']);
  128. }
  129. /**
  130. * @covers Monolog\Formatter\LogstashFormatter::format
  131. */
  132. public function testDefaultFormatterV1()
  133. {
  134. $formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1);
  135. $record = array(
  136. 'level' => Logger::ERROR,
  137. 'level_name' => 'ERROR',
  138. 'channel' => 'meh',
  139. 'context' => array(),
  140. 'datetime' => new \DateTime("@0"),
  141. 'extra' => array(),
  142. 'message' => 'log',
  143. );
  144. $message = json_decode($formatter->format($record), true);
  145. $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
  146. $this->assertEquals("1", $message['@version']);
  147. $this->assertEquals('log', $message['message']);
  148. $this->assertEquals('meh', $message['channel']);
  149. $this->assertEquals('ERROR', $message['level']);
  150. $this->assertEquals('test', $message['type']);
  151. $this->assertEquals('hostname', $message['host']);
  152. $formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1);
  153. $message = json_decode($formatter->format($record), true);
  154. $this->assertEquals('mysystem', $message['type']);
  155. }
  156. /**
  157. * @covers Monolog\Formatter\LogstashFormatter::format
  158. */
  159. public function testFormatWithFileAndLineV1()
  160. {
  161. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  162. $record = array(
  163. 'level' => Logger::ERROR,
  164. 'level_name' => 'ERROR',
  165. 'channel' => 'meh',
  166. 'context' => array('from' => 'logger'),
  167. 'datetime' => new \DateTime("@0"),
  168. 'extra' => array('file' => 'test', 'line' => 14),
  169. 'message' => 'log',
  170. );
  171. $message = json_decode($formatter->format($record), true);
  172. $this->assertEquals('test', $message['file']);
  173. $this->assertEquals(14, $message['line']);
  174. }
  175. /**
  176. * @covers Monolog\Formatter\LogstashFormatter::format
  177. */
  178. public function testFormatWithContextV1()
  179. {
  180. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  181. $record = array(
  182. 'level' => Logger::ERROR,
  183. 'level_name' => 'ERROR',
  184. 'channel' => 'meh',
  185. 'context' => array('from' => 'logger'),
  186. 'datetime' => new \DateTime("@0"),
  187. 'extra' => array('key' => 'pair'),
  188. 'message' => 'log'
  189. );
  190. $message = json_decode($formatter->format($record), true);
  191. $this->assertArrayHasKey('ctxt_from', $message);
  192. $this->assertEquals('logger', $message['ctxt_from']);
  193. // Test with extraPrefix
  194. $formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1);
  195. $message = json_decode($formatter->format($record), true);
  196. $this->assertArrayHasKey('CTXfrom', $message);
  197. $this->assertEquals('logger', $message['CTXfrom']);
  198. }
  199. /**
  200. * @covers Monolog\Formatter\LogstashFormatter::format
  201. */
  202. public function testFormatWithExtraV1()
  203. {
  204. $formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
  205. $record = array(
  206. 'level' => Logger::ERROR,
  207. 'level_name' => 'ERROR',
  208. 'channel' => 'meh',
  209. 'context' => array('from' => 'logger'),
  210. 'datetime' => new \DateTime("@0"),
  211. 'extra' => array('key' => 'pair'),
  212. 'message' => 'log'
  213. );
  214. $message = json_decode($formatter->format($record), true);
  215. $this->assertArrayHasKey('key', $message);
  216. $this->assertEquals('pair', $message['key']);
  217. // Test with extraPrefix
  218. $formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1);
  219. $message = json_decode($formatter->format($record), true);
  220. $this->assertArrayHasKey('EXTkey', $message);
  221. $this->assertEquals('pair', $message['EXTkey']);
  222. }
  223. public function testFormatWithApplicationNameV1()
  224. {
  225. $formatter = new LogstashFormatter('app', 'test', 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. $message = json_decode($formatter->format($record), true);
  236. $this->assertArrayHasKey('type', $message);
  237. $this->assertEquals('app', $message['type']);
  238. }
  239. }