LineFormatterTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /**
  12. * @covers Monolog\Formatter\LineFormatter
  13. */
  14. class LineFormatterTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testDefFormatWithString()
  17. {
  18. $formatter = new LineFormatter(null, 'Y-m-d');
  19. $message = $formatter->format(array(
  20. 'level_name' => 'WARNING',
  21. 'channel' => 'log',
  22. 'context' => array(),
  23. 'message' => 'foo',
  24. 'datetime' => new \DateTime,
  25. 'extra' => array(),
  26. ));
  27. $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  28. }
  29. public function testDefFormatWithArrayContext()
  30. {
  31. $formatter = new LineFormatter(null, 'Y-m-d');
  32. $message = $formatter->format(array(
  33. 'level_name' => 'ERROR',
  34. 'channel' => 'meh',
  35. 'message' => 'foo',
  36. 'datetime' => new \DateTime,
  37. 'extra' => array(),
  38. 'context' => array(
  39. 'foo' => 'bar',
  40. 'baz' => 'qux',
  41. 'bool' => false,
  42. 'null' => null,
  43. )
  44. ));
  45. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
  46. }
  47. public function testDefFormatExtras()
  48. {
  49. $formatter = new LineFormatter(null, 'Y-m-d');
  50. $message = $formatter->format(array(
  51. 'level_name' => 'ERROR',
  52. 'channel' => 'meh',
  53. 'context' => array(),
  54. 'datetime' => new \DateTime,
  55. 'extra' => array('ip' => '127.0.0.1'),
  56. 'message' => 'log',
  57. ));
  58. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
  59. }
  60. public function testFormatExtras()
  61. {
  62. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
  63. $message = $formatter->format(array(
  64. 'level_name' => 'ERROR',
  65. 'channel' => 'meh',
  66. 'context' => array(),
  67. 'datetime' => new \DateTime,
  68. 'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
  69. 'message' => 'log',
  70. ));
  71. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
  72. }
  73. public function testDefFormatWithObject()
  74. {
  75. $formatter = new LineFormatter(null, 'Y-m-d');
  76. $message = $formatter->format(array(
  77. 'level_name' => 'ERROR',
  78. 'channel' => 'meh',
  79. 'context' => array(),
  80. 'datetime' => new \DateTime,
  81. 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
  82. 'message' => 'foobar',
  83. ));
  84. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: {})","baz":[],"res":"[resource]"}'."\n", $message);
  85. }
  86. public function testDefFormatWithException()
  87. {
  88. $formatter = new LineFormatter(null, 'Y-m-d');
  89. $message = $formatter->format(array(
  90. 'level_name' => 'CRITICAL',
  91. 'channel' => 'core',
  92. 'context' => array('exception' => new \RuntimeException('Foo')),
  93. 'datetime' => new \DateTime,
  94. 'extra' => array(),
  95. 'message' => 'foobar',
  96. ));
  97. $path = str_replace('\\/', '/', json_encode(__FILE__));
  98. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException: Foo at '.substr($path, 1, -1).':'.(__LINE__-8).')"} []'."\n", $message);
  99. }
  100. public function testDefFormatWithPreviousException()
  101. {
  102. $formatter = new LineFormatter(null, 'Y-m-d');
  103. $previous = new \LogicException('Wut?');
  104. $message = $formatter->format(array(
  105. 'level_name' => 'CRITICAL',
  106. 'channel' => 'core',
  107. 'context' => array('exception' => new \RuntimeException('Foo', 0, $previous)),
  108. 'datetime' => new \DateTime,
  109. 'extra' => array(),
  110. 'message' => 'foobar',
  111. ));
  112. $path = str_replace('\\/', '/', json_encode(__FILE__));
  113. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException: Foo at '.substr($path, 1, -1).':'.(__LINE__-8).', LogicException: Wut? at '.substr($path, 1, -1).':'.(__LINE__-12).')"} []'."\n", $message);
  114. }
  115. public function testBatchFormat()
  116. {
  117. $formatter = new LineFormatter(null, 'Y-m-d');
  118. $message = $formatter->formatBatch(array(
  119. array(
  120. 'level_name' => 'CRITICAL',
  121. 'channel' => 'test',
  122. 'message' => 'bar',
  123. 'context' => array(),
  124. 'datetime' => new \DateTime,
  125. 'extra' => array(),
  126. ),
  127. array(
  128. 'level_name' => 'WARNING',
  129. 'channel' => 'log',
  130. 'message' => 'foo',
  131. 'context' => array(),
  132. 'datetime' => new \DateTime,
  133. 'extra' => array(),
  134. ),
  135. ));
  136. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  137. }
  138. public function testFormatShouldStripInlineLineBreaks()
  139. {
  140. $formatter = new LineFormatter(null, 'Y-m-d');
  141. $message = $formatter->format(
  142. array(
  143. 'message' => "foo\nbar",
  144. 'context' => array(),
  145. 'extra' => array(),
  146. )
  147. );
  148. $this->assertRegExp('/foo bar/', $message);
  149. }
  150. public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
  151. {
  152. $formatter = new LineFormatter(null, 'Y-m-d', true);
  153. $message = $formatter->format(
  154. array(
  155. 'message' => "foo\nbar",
  156. 'context' => array(),
  157. 'extra' => array(),
  158. )
  159. );
  160. $this->assertRegExp('/foo\nbar/', $message);
  161. }
  162. }
  163. class TestFoo
  164. {
  165. public $foo = 'foo';
  166. }
  167. class TestBar
  168. {
  169. public function __toString()
  170. {
  171. return 'bar';
  172. }
  173. }