4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Formatter;
15 * @covers Monolog\Formatter\LineFormatter
17 class LineFormatterTest extends \PHPUnit_Framework_TestCase
19 public function testDefFormatWithString()
21 $formatter = new LineFormatter(null, 'Y-m-d');
22 $message = $formatter->format(array(
23 'level_name' => 'WARNING',
27 'datetime' => new \DateTime,
30 $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
33 public function testDefFormatWithArrayContext()
35 $formatter = new LineFormatter(null, 'Y-m-d');
36 $message = $formatter->format(array(
37 'level_name' => 'ERROR',
40 'datetime' => new \DateTime,
49 $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
52 public function testDefFormatExtras()
54 $formatter = new LineFormatter(null, 'Y-m-d');
55 $message = $formatter->format(array(
56 'level_name' => 'ERROR',
59 'datetime' => new \DateTime,
60 'extra' => array('ip' => '127.0.0.1'),
63 $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
66 public function testFormatExtras()
68 $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
69 $message = $formatter->format(array(
70 'level_name' => 'ERROR',
73 'datetime' => new \DateTime,
74 'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
77 $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
80 public function testContextAndExtraOptionallyNotShownIfEmpty()
82 $formatter = new LineFormatter(null, 'Y-m-d', false, true);
83 $message = $formatter->format(array(
84 'level_name' => 'ERROR',
87 'datetime' => new \DateTime,
91 $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
94 public function testContextAndExtraReplacement()
96 $formatter = new LineFormatter('%context.foo% => %extra.foo%');
97 $message = $formatter->format(array(
98 'level_name' => 'ERROR',
100 'context' => array('foo' => 'bar'),
101 'datetime' => new \DateTime,
102 'extra' => array('foo' => 'xbar'),
105 $this->assertEquals('bar => xbar', $message);
108 public function testDefFormatWithObject()
110 $formatter = new LineFormatter(null, 'Y-m-d');
111 $message = $formatter->format(array(
112 'level_name' => 'ERROR',
114 'context' => array(),
115 'datetime' => new \DateTime,
116 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
117 'message' => 'foobar',
120 $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: bar)","baz":[],"res":"[resource] (stream)"}'."\n", $message);
123 public function testDefFormatWithException()
125 $formatter = new LineFormatter(null, 'Y-m-d');
126 $message = $formatter->format(array(
127 'level_name' => 'CRITICAL',
129 'context' => array('exception' => new \RuntimeException('Foo')),
130 'datetime' => new \DateTime,
132 'message' => 'foobar',
135 $path = str_replace('\\/', '/', json_encode(__FILE__));
137 $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
140 public function testDefFormatWithPreviousException()
142 $formatter = new LineFormatter(null, 'Y-m-d');
143 $previous = new \LogicException('Wut?');
144 $message = $formatter->format(array(
145 'level_name' => 'CRITICAL',
147 'context' => array('exception' => new \RuntimeException('Foo', 0, $previous)),
148 'datetime' => new \DateTime,
150 'message' => 'foobar',
153 $path = str_replace('\\/', '/', json_encode(__FILE__));
155 $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).', LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__ - 12).')"} []'."\n", $message);
158 public function testBatchFormat()
160 $formatter = new LineFormatter(null, 'Y-m-d');
161 $message = $formatter->formatBatch(array(
163 'level_name' => 'CRITICAL',
166 'context' => array(),
167 'datetime' => new \DateTime,
171 'level_name' => 'WARNING',
174 'context' => array(),
175 'datetime' => new \DateTime,
179 $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
182 public function testFormatShouldStripInlineLineBreaks()
184 $formatter = new LineFormatter(null, 'Y-m-d');
185 $message = $formatter->format(
187 'message' => "foo\nbar",
188 'context' => array(),
193 $this->assertRegExp('/foo bar/', $message);
196 public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
198 $formatter = new LineFormatter(null, 'Y-m-d', true);
199 $message = $formatter->format(
201 'message' => "foo\nbar",
202 'context' => array(),
207 $this->assertRegExp('/foo\nbar/', $message);
218 public function __toString()