LineFormatterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /**
  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([
  20. 'level_name' => 'WARNING',
  21. 'channel' => 'log',
  22. 'context' => [],
  23. 'message' => 'foo',
  24. 'datetime' => new \DateTimeImmutable,
  25. 'extra' => [],
  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([
  33. 'level_name' => 'ERROR',
  34. 'channel' => 'meh',
  35. 'message' => 'foo',
  36. 'datetime' => new \DateTimeImmutable,
  37. 'extra' => [],
  38. 'context' => [
  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([
  51. 'level_name' => 'ERROR',
  52. 'channel' => 'meh',
  53. 'context' => [],
  54. 'datetime' => new \DateTimeImmutable,
  55. 'extra' => ['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([
  64. 'level_name' => 'ERROR',
  65. 'channel' => 'meh',
  66. 'context' => [],
  67. 'datetime' => new \DateTimeImmutable,
  68. 'extra' => ['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 testContextAndExtraOptionallyNotShownIfEmpty()
  74. {
  75. $formatter = new LineFormatter(null, 'Y-m-d', false, true);
  76. $message = $formatter->format([
  77. 'level_name' => 'ERROR',
  78. 'channel' => 'meh',
  79. 'context' => [],
  80. 'datetime' => new \DateTimeImmutable,
  81. 'extra' => [],
  82. 'message' => 'log',
  83. ]);
  84. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
  85. }
  86. public function testContextAndExtraReplacement()
  87. {
  88. $formatter = new LineFormatter('%context.foo% => %extra.foo%');
  89. $message = $formatter->format([
  90. 'level_name' => 'ERROR',
  91. 'channel' => 'meh',
  92. 'context' => ['foo' => 'bar'],
  93. 'datetime' => new \DateTimeImmutable,
  94. 'extra' => ['foo' => 'xbar'],
  95. 'message' => 'log',
  96. ]);
  97. $this->assertEquals('bar => xbar', $message);
  98. }
  99. public function testDefFormatWithObject()
  100. {
  101. $formatter = new LineFormatter(null, 'Y-m-d');
  102. $message = $formatter->format([
  103. 'level_name' => 'ERROR',
  104. 'channel' => 'meh',
  105. 'context' => [],
  106. 'datetime' => new \DateTimeImmutable,
  107. 'extra' => ['foo' => new TestFoo, 'bar' => new TestBar, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
  108. 'message' => 'foobar',
  109. ]);
  110. $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);
  111. }
  112. public function testDefFormatWithException()
  113. {
  114. $formatter = new LineFormatter(null, 'Y-m-d');
  115. $message = $formatter->format([
  116. 'level_name' => 'CRITICAL',
  117. 'channel' => 'core',
  118. 'context' => ['exception' => new \RuntimeException('Foo')],
  119. 'datetime' => new \DateTimeImmutable,
  120. 'extra' => [],
  121. 'message' => 'foobar',
  122. ]);
  123. $path = str_replace('\\/', '/', json_encode(__FILE__));
  124. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
  125. }
  126. public function testDefFormatWithExceptionAndStacktrace()
  127. {
  128. $formatter = new LineFormatter(null, 'Y-m-d');
  129. $formatter->includeStacktraces();
  130. $message = $formatter->format([
  131. 'level_name' => 'CRITICAL',
  132. 'channel' => 'core',
  133. 'context' => ['exception' => new \RuntimeException('Foo')],
  134. 'datetime' => new \DateTimeImmutable,
  135. 'extra' => [],
  136. 'message' => 'foobar',
  137. ]);
  138. $path = str_replace('\\/', '/', json_encode(__FILE__));
  139. $this->assertRegexp('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 8).'\)\n\[stacktrace]\n#0}', $message);
  140. }
  141. public function testInlineLineBreaksRespectsEscapedBackslashes()
  142. {
  143. $formatter = new LineFormatter(null, 'Y-m-d');
  144. $formatter->allowInlineLineBreaks();
  145. self::assertSame('{"test":"foo'."\n".'bar\\\\name-with-n"}', $formatter->stringify(["test" => "foo\nbar\\name-with-n"]));
  146. }
  147. public function testDefFormatWithExceptionAndStacktraceParserFull()
  148. {
  149. $formatter = new LineFormatter(null, 'Y-m-d');
  150. $formatter->includeStacktraces(true, function ($line) {
  151. return $line;
  152. });
  153. $message = $formatter->format([
  154. 'level_name' => 'CRITICAL',
  155. 'channel' => 'core',
  156. 'context' => ['exception' => new \RuntimeException('Foo')],
  157. 'datetime' => new \DateTimeImmutable,
  158. 'extra' => [],
  159. 'message' => 'foobar',
  160. ]);
  161. $trace = explode('[stacktrace]', $message, 2)[1];
  162. $this->assertStringContainsString('TestCase.php', $trace);
  163. $this->assertStringContainsString('TestResult.php', $trace);
  164. }
  165. public function testDefFormatWithExceptionAndStacktraceParserCustom()
  166. {
  167. $formatter = new LineFormatter(null, 'Y-m-d');
  168. $formatter->includeStacktraces(true, function ($line) {
  169. if (strpos($line, 'TestCase.php') === false) {
  170. return $line;
  171. }
  172. });
  173. $message = $formatter->format([
  174. 'level_name' => 'CRITICAL',
  175. 'channel' => 'core',
  176. 'context' => ['exception' => new \RuntimeException('Foo')],
  177. 'datetime' => new \DateTimeImmutable,
  178. 'extra' => [],
  179. 'message' => 'foobar',
  180. ]);
  181. $trace = explode('[stacktrace]', $message, 2)[1];
  182. $this->assertStringNotContainsString('TestCase.php', $trace);
  183. $this->assertStringContainsString('TestResult.php', $trace);
  184. }
  185. public function testDefFormatWithExceptionAndStacktraceParserEmpty()
  186. {
  187. $formatter = new LineFormatter(null, 'Y-m-d');
  188. $formatter->includeStacktraces(true, function ($line) {
  189. return null;
  190. });
  191. $message = $formatter->format([
  192. 'level_name' => 'CRITICAL',
  193. 'channel' => 'core',
  194. 'context' => ['exception' => new \RuntimeException('Foo')],
  195. 'datetime' => new \DateTimeImmutable,
  196. 'extra' => [],
  197. 'message' => 'foobar',
  198. ]);
  199. $trace = explode('[stacktrace]', $message, 2)[1];
  200. $this->assertStringNotContainsString('#', $trace);
  201. }
  202. public function testDefFormatWithPreviousException()
  203. {
  204. $formatter = new LineFormatter(null, 'Y-m-d');
  205. $previous = new \LogicException('Wut?');
  206. $message = $formatter->format([
  207. 'level_name' => 'CRITICAL',
  208. 'channel' => 'core',
  209. 'context' => ['exception' => new \RuntimeException('Foo', 0, $previous)],
  210. 'datetime' => new \DateTimeImmutable,
  211. 'extra' => [],
  212. 'message' => 'foobar',
  213. ]);
  214. $path = str_replace('\\/', '/', json_encode(__FILE__));
  215. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')\n[previous exception] [object] (LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 12).')"} []'."\n", $message);
  216. }
  217. public function testDefFormatWithSoapFaultException()
  218. {
  219. if (!class_exists('SoapFault')) {
  220. $this->markTestSkipped('Requires the soap extension');
  221. }
  222. $formatter = new LineFormatter(null, 'Y-m-d');
  223. $message = $formatter->format([
  224. 'level_name' => 'CRITICAL',
  225. 'channel' => 'core',
  226. 'context' => ['exception' => new \SoapFault('foo', 'bar', 'hello', 'world')],
  227. 'datetime' => new \DateTimeImmutable,
  228. 'extra' => [],
  229. 'message' => 'foobar',
  230. ]);
  231. $path = str_replace('\\/', '/', json_encode(__FILE__));
  232. $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__ - 8).')"} []'."\n", $message);
  233. $message = $formatter->format([
  234. 'level_name' => 'CRITICAL',
  235. 'channel' => 'core',
  236. 'context' => ['exception' => new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world'])],
  237. 'datetime' => new \DateTimeImmutable,
  238. 'extra' => [],
  239. 'message' => 'foobar',
  240. ]);
  241. $path = str_replace('\\/', '/', json_encode(__FILE__));
  242. $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__ - 8).')"} []'."\n", $message);
  243. }
  244. public function testBatchFormat()
  245. {
  246. $formatter = new LineFormatter(null, 'Y-m-d');
  247. $message = $formatter->formatBatch([
  248. [
  249. 'level_name' => 'CRITICAL',
  250. 'channel' => 'test',
  251. 'message' => 'bar',
  252. 'context' => [],
  253. 'datetime' => new \DateTimeImmutable,
  254. 'extra' => [],
  255. ],
  256. [
  257. 'level_name' => 'WARNING',
  258. 'channel' => 'log',
  259. 'message' => 'foo',
  260. 'context' => [],
  261. 'datetime' => new \DateTimeImmutable,
  262. 'extra' => [],
  263. ],
  264. ]);
  265. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  266. }
  267. public function testFormatShouldStripInlineLineBreaks()
  268. {
  269. $formatter = new LineFormatter(null, 'Y-m-d');
  270. $message = $formatter->format(
  271. [
  272. 'message' => "foo\nbar",
  273. 'context' => [],
  274. 'extra' => [],
  275. ]
  276. );
  277. $this->assertRegExp('/foo bar/', $message);
  278. }
  279. public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
  280. {
  281. $formatter = new LineFormatter(null, 'Y-m-d', true);
  282. $message = $formatter->format(
  283. [
  284. 'message' => "foo\nbar",
  285. 'context' => [],
  286. 'extra' => [],
  287. ]
  288. );
  289. $this->assertRegExp('/foo\nbar/', $message);
  290. }
  291. }
  292. class TestFoo
  293. {
  294. public $foo = 'fooValue';
  295. }
  296. class TestBar
  297. {
  298. public function __toString()
  299. {
  300. return 'bar';
  301. }
  302. }