LineFormatterTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  83. $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);
  84. } else {
  85. $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);
  86. }
  87. }
  88. public function testBatchFormat()
  89. {
  90. $formatter = new LineFormatter(null, 'Y-m-d');
  91. $message = $formatter->formatBatch(array(
  92. array(
  93. 'level_name' => 'CRITICAL',
  94. 'channel' => 'test',
  95. 'message' => 'bar',
  96. 'context' => array(),
  97. 'datetime' => new \DateTime,
  98. 'extra' => array(),
  99. ),
  100. array(
  101. 'level_name' => 'WARNING',
  102. 'channel' => 'log',
  103. 'message' => 'foo',
  104. 'context' => array(),
  105. 'datetime' => new \DateTime,
  106. 'extra' => array(),
  107. ),
  108. ));
  109. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  110. }
  111. }
  112. class TestFoo
  113. {
  114. public $foo = 'foo';
  115. }
  116. class TestBar
  117. {
  118. public function __toString()
  119. {
  120. return 'bar';
  121. }
  122. }