LineFormatterTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. )
  42. ));
  43. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message);
  44. }
  45. public function testDefFormatExtras()
  46. {
  47. $formatter = new LineFormatter(null, 'Y-m-d');
  48. $message = $formatter->format(array(
  49. 'level_name' => 'ERROR',
  50. 'channel' => 'meh',
  51. 'context' => array(),
  52. 'datetime' => new \DateTime,
  53. 'extra' => array('ip' => '127.0.0.1'),
  54. 'message' => 'log',
  55. ));
  56. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
  57. }
  58. public function testFormatExtras()
  59. {
  60. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
  61. $message = $formatter->format(array(
  62. 'level_name' => 'ERROR',
  63. 'channel' => 'meh',
  64. 'context' => array(),
  65. 'datetime' => new \DateTime,
  66. 'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
  67. 'message' => 'log',
  68. ));
  69. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
  70. }
  71. public function testDefFormatWithObject()
  72. {
  73. $formatter = new LineFormatter(null, 'Y-m-d');
  74. $message = $formatter->format(array(
  75. 'level_name' => 'ERROR',
  76. 'channel' => 'meh',
  77. 'context' => array(),
  78. 'datetime' => new \DateTime,
  79. 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
  80. 'message' => 'foobar',
  81. ));
  82. $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);
  83. }
  84. public function testDefFormatWithException()
  85. {
  86. $formatter = new LineFormatter(null, 'Y-m-d');
  87. $message = $formatter->format(array(
  88. 'level_name' => 'CRITICAL',
  89. 'channel' => 'core',
  90. 'context' => array('exception' => new \RuntimeException('Foo')),
  91. 'datetime' => new \DateTime,
  92. 'extra' => array(),
  93. 'message' => 'foobar',
  94. ));
  95. $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException: Foo at '.substr(json_encode(__FILE__), 1, -1).':'.(__LINE__-6).')"} []'."\n", $message);
  96. }
  97. public function testBatchFormat()
  98. {
  99. $formatter = new LineFormatter(null, 'Y-m-d');
  100. $message = $formatter->formatBatch(array(
  101. array(
  102. 'level_name' => 'CRITICAL',
  103. 'channel' => 'test',
  104. 'message' => 'bar',
  105. 'context' => array(),
  106. 'datetime' => new \DateTime,
  107. 'extra' => array(),
  108. ),
  109. array(
  110. 'level_name' => 'WARNING',
  111. 'channel' => 'log',
  112. 'message' => 'foo',
  113. 'context' => array(),
  114. 'datetime' => new \DateTime,
  115. 'extra' => array(),
  116. ),
  117. ));
  118. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  119. }
  120. }
  121. class TestFoo
  122. {
  123. public $foo = 'foo';
  124. }
  125. class TestBar
  126. {
  127. public function __toString()
  128. {
  129. return 'bar';
  130. }
  131. }