LineFormatterTest.php 3.4 KB

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