LineFormatterTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Level;
  13. /**
  14. * @covers Monolog\Formatter\LineFormatter
  15. */
  16. class LineFormatterTest extends TestCase
  17. {
  18. public function testDefFormatWithString()
  19. {
  20. $formatter = new LineFormatter(null, 'Y-m-d');
  21. $message = $formatter->format($this->getRecord(
  22. Level::Warning,
  23. 'foo',
  24. channel: 'log',
  25. ));
  26. $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  27. }
  28. public function testDefFormatWithArrayContext()
  29. {
  30. $formatter = new LineFormatter(null, 'Y-m-d');
  31. $message = $formatter->format($this->getRecord(
  32. Level::Error,
  33. 'foo',
  34. channel: 'meh',
  35. context: [
  36. 'foo' => 'bar',
  37. 'baz' => 'qux',
  38. 'bool' => false,
  39. 'null' => null,
  40. ],
  41. ));
  42. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
  43. }
  44. public function testDefFormatExtras()
  45. {
  46. $formatter = new LineFormatter(null, 'Y-m-d');
  47. $message = $formatter->format($this->getRecord(
  48. Level::Error,
  49. 'log',
  50. channel: 'meh',
  51. extra: ['ip' => '127.0.0.1'],
  52. ));
  53. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
  54. }
  55. public function testFormatExtras()
  56. {
  57. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
  58. $message = $formatter->format($this->getRecord(
  59. Level::Error,
  60. 'log',
  61. channel: 'meh',
  62. extra: ['ip' => '127.0.0.1', 'file' => 'test'],
  63. ));
  64. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
  65. }
  66. public function testContextAndExtraOptionallyNotShownIfEmpty()
  67. {
  68. $formatter = new LineFormatter(null, 'Y-m-d', false, true);
  69. $message = $formatter->format($this->getRecord(
  70. Level::Error,
  71. 'log',
  72. channel: 'meh',
  73. ));
  74. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
  75. }
  76. public function testContextAndExtraReplacement()
  77. {
  78. $formatter = new LineFormatter('%context.foo% => %extra.foo%');
  79. $message = $formatter->format($this->getRecord(
  80. Level::Error,
  81. 'log',
  82. channel: 'meh',
  83. context: ['foo' => 'bar'],
  84. extra: ['foo' => 'xbar'],
  85. ));
  86. $this->assertEquals('bar => xbar', $message);
  87. }
  88. public function testDefFormatWithObject()
  89. {
  90. $formatter = new LineFormatter(null, 'Y-m-d');
  91. $message = $formatter->format($this->getRecord(
  92. Level::Error,
  93. 'foobar',
  94. channel: 'meh',
  95. context: [],
  96. extra: ['foo' => new TestFoo, 'bar' => new TestBar, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
  97. ));
  98. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":{"Monolog\\\\Formatter\\\\TestFoo":{"foo":"fooValue"}},"bar":{"Monolog\\\\Formatter\\\\TestBar":"bar"},"baz":[],"res":"[resource(stream)]"}'."\n", $message);
  99. }
  100. public function testDefFormatWithException()
  101. {
  102. $formatter = new LineFormatter(null, 'Y-m-d');
  103. $message = $formatter->format($this->getRecord(
  104. Level::Critical,
  105. 'foobar',
  106. channel: 'core',
  107. context: ['exception' => new \RuntimeException('Foo')],
  108. ));
  109. $path = str_replace('\\/', '/', json_encode(__FILE__));
  110. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
  111. }
  112. public function testDefFormatWithExceptionAndStacktrace()
  113. {
  114. $formatter = new LineFormatter(null, 'Y-m-d');
  115. $formatter->includeStacktraces();
  116. $message = $formatter->format($this->getRecord(
  117. Level::Critical,
  118. 'foobar',
  119. channel: 'core',
  120. context: ['exception' => new \RuntimeException('Foo')],
  121. ));
  122. $path = str_replace('\\/', '/', json_encode(__FILE__));
  123. $this->assertMatchesRegularExpression('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 5).'\)\n\[stacktrace]\n#0}', $message);
  124. }
  125. public function testDefFormatWithPreviousException()
  126. {
  127. $formatter = new LineFormatter(null, 'Y-m-d');
  128. $previous = new \LogicException('Wut?');
  129. $message = $formatter->format($this->getRecord(
  130. Level::Critical,
  131. 'foobar',
  132. channel: 'core',
  133. context: ['exception' => new \RuntimeException('Foo', 0, $previous)],
  134. ));
  135. $path = str_replace('\\/', '/', json_encode(__FILE__));
  136. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 5).')\n[previous exception] [object] (LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 10).')"} []'."\n", $message);
  137. }
  138. public function testDefFormatWithSoapFaultException()
  139. {
  140. if (!class_exists('SoapFault')) {
  141. $this->markTestSkipped('Requires the soap extension');
  142. }
  143. $formatter = new LineFormatter(null, 'Y-m-d');
  144. $message = $formatter->format($this->getRecord(
  145. Level::Critical,
  146. 'foobar',
  147. channel: 'core',
  148. context: ['exception' => new \SoapFault('foo', 'bar', 'hello', 'world')],
  149. ));
  150. $path = str_replace('\\/', '/', json_encode(__FILE__));
  151. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: world): bar at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
  152. $message = $formatter->format($this->getRecord(
  153. Level::Critical,
  154. 'foobar',
  155. channel: 'core',
  156. context: ['exception' => new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world'])],
  157. ));
  158. $path = str_replace('\\/', '/', json_encode(__FILE__));
  159. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: {\"bar\":{\"biz\":\"baz\"},\"foo\":\"world\"}): bar at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
  160. }
  161. public function testBatchFormat()
  162. {
  163. $formatter = new LineFormatter(null, 'Y-m-d');
  164. $message = $formatter->formatBatch([
  165. $this->getRecord(
  166. Level::Critical,
  167. 'bar',
  168. channel: 'test',
  169. ),
  170. $this->getRecord(
  171. Level::Warning,
  172. 'foo',
  173. channel: 'log',
  174. ),
  175. ]);
  176. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  177. }
  178. public function testFormatShouldStripInlineLineBreaks()
  179. {
  180. $formatter = new LineFormatter(null, 'Y-m-d');
  181. $message = $formatter->format($this->getRecord(message: "foo\nbar"));
  182. $this->assertMatchesRegularExpression('/foo bar/', $message);
  183. }
  184. public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
  185. {
  186. $formatter = new LineFormatter(null, 'Y-m-d', true);
  187. $message = $formatter->format($this->getRecord(message: "foo\nbar"));
  188. $this->assertMatchesRegularExpression('/foo\nbar/', $message);
  189. }
  190. }
  191. class TestFoo
  192. {
  193. public string $foo = 'fooValue';
  194. }
  195. class TestBar
  196. {
  197. public function __toString()
  198. {
  199. return 'bar';
  200. }
  201. }