LineFormatterTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. use Monolog\Logger;
  12. /**
  13. * @covers Monolog\Formatter\LineFormatter
  14. */
  15. class LineFormatterTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testDefFormatWithString()
  18. {
  19. $formatter = new LineFormatter(null, 'Y-m-d');
  20. $message = $formatter->format(array(
  21. 'level_name' => 'WARNING',
  22. 'channel' => 'log',
  23. 'context' => array(),
  24. 'message' => 'foo',
  25. 'datetime' => new \DateTime,
  26. 'extra' => array(),
  27. ));
  28. $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  29. }
  30. public function testDefFormatWithArrayContext()
  31. {
  32. $formatter = new LineFormatter(null, 'Y-m-d');
  33. $message = $formatter->format(array(
  34. 'level_name' => 'ERROR',
  35. 'channel' => 'meh',
  36. 'message' => 'foo',
  37. 'datetime' => new \DateTime,
  38. 'extra' => array(),
  39. 'context' => array(
  40. 'foo' => 'bar',
  41. 'baz' => 'qux',
  42. )
  43. ));
  44. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message);
  45. }
  46. public function testDefFormatExtras()
  47. {
  48. $formatter = new LineFormatter(null, 'Y-m-d');
  49. $message = $formatter->format(array(
  50. 'level_name' => 'ERROR',
  51. 'channel' => 'meh',
  52. 'context' => array(),
  53. 'datetime' => new \DateTime,
  54. 'extra' => array('ip' => '127.0.0.1'),
  55. 'message' => 'log',
  56. ));
  57. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
  58. }
  59. public function testFormatExtras()
  60. {
  61. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
  62. $message = $formatter->format(array(
  63. 'level_name' => 'ERROR',
  64. 'channel' => 'meh',
  65. 'context' => array(),
  66. 'datetime' => new \DateTime,
  67. 'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
  68. 'message' => 'log',
  69. ));
  70. $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
  71. }
  72. public function testDefFormatWithObject()
  73. {
  74. $formatter = new LineFormatter(null, 'Y-m-d');
  75. $message = $formatter->format(array(
  76. 'level_name' => 'ERROR',
  77. 'channel' => 'meh',
  78. 'context' => array(),
  79. 'datetime' => new \DateTime,
  80. 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
  81. 'message' => 'foobar',
  82. ));
  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. }
  85. public function testBatchFormat()
  86. {
  87. $formatter = new LineFormatter(null, 'Y-m-d');
  88. $message = $formatter->formatBatch(array(
  89. array(
  90. 'level_name' => 'CRITICAL',
  91. 'channel' => 'test',
  92. 'message' => 'bar',
  93. 'context' => array(),
  94. 'datetime' => new \DateTime,
  95. 'extra' => array(),
  96. ),
  97. array(
  98. 'level_name' => 'WARNING',
  99. 'channel' => 'log',
  100. 'message' => 'foo',
  101. 'context' => array(),
  102. 'datetime' => new \DateTime,
  103. 'extra' => array(),
  104. ),
  105. ));
  106. $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
  107. }
  108. }
  109. class TestFoo
  110. {
  111. public $foo = 'foo';
  112. }
  113. class TestBar
  114. {
  115. public function __toString()
  116. {
  117. return 'bar';
  118. }
  119. }