1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 14:16:42 +02:00

Added the support of a logging context

This commit is contained in:
Christophe Coevoet
2011-06-01 00:10:38 +02:00
parent a8d4fc4a34
commit c1675c59d8
13 changed files with 74 additions and 60 deletions

View File

@@ -21,6 +21,7 @@ class JsonFormatterTest extends \PHPUnit_Framework_TestCase
$message = $formatter->format($msg = array(
'level_name' => 'WARNING',
'channel' => 'log',
'context' => array(),
'message' => array('foo'),
'datetime' => new \DateTime,
'extra' => array(),

View File

@@ -21,27 +21,29 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
$message = $formatter->format(array(
'level_name' => 'WARNING',
'channel' => 'log',
'context' => array(),
'message' => 'foo',
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo '."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
}
public function testDefFormatWithArray()
public function testDefFormatWithArrayContext()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'message' => 'foo',
'datetime' => new \DateTime,
'extra' => array(),
'message' => array(
'context' => array(
'foo' => 'bar',
'baz' => 'qux',
)
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: message(foo: bar, baz: qux) '."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux"} []'."\n", $message);
}
public function testDefFormatExtras()
@@ -50,11 +52,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
$message = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array('ip' => '127.0.0.1'),
'message' => 'log',
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log extra(ip: 127.0.0.1)'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
}
public function testDefFormatWithObject()
@@ -63,11 +66,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
$message = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array()),
'message' => 'foobar',
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar extra(foo: O:25:"Monolog\\Formatter\\TestFoo":1:{s:3:"foo";s:3:"foo";}, bar: bar, baz: a:0:{})'."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\Formatter\\TestFoo: {"foo":"foo"})","bar":"[object] (Monolog\\Formatter\\TestBar: {})","baz":[]}'."\n", $message);
}
public function testBatchFormat()
@@ -78,6 +82,7 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array(),
),
@@ -85,11 +90,12 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array(),
),
));
$this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar '."\n".'['.date('Y-m-d').'] log.WARNING: foo '."\n", $message);
$this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
}
}

View File

@@ -15,34 +15,24 @@ use Monolog\Logger;
class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider recordProvider
*/
public function testDefaultFormatIsLineFormatterWithoutNewLine($record)
public function testDefaultFormatIsLineFormatterWithoutNewLine()
{
$wildfire = new WildfireFormatter();
$message = $wildfire->format($record);
$this->assertEquals(
'70|[{"Type":"ERROR","File":"","Line":""},"meh: log extra(ip: 127.0.0.1)"]|',
$message
);
}
public function recordProvider()
{
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array('ip' => '127.0.0.1'),
'message' => 'log',
);
return array(
array($record),
$message = $wildfire->format($record);
$this->assertEquals(
'75|[{"Type":"ERROR","File":"","Line":""},"meh: log [] {\\"ip\\":\\"127.0.0.1\\"}"]|',
$message
);
}
}

View File

@@ -31,8 +31,8 @@ class FirePHPHandlerTest extends TestCase
'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2',
'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1',
'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3',
'X-Wf-1-1-1-1' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-2' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|',
'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|',
);
$this->assertEquals($expected, $handler->getHeaders());
@@ -52,13 +52,13 @@ class FirePHPHandlerTest extends TestCase
'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2',
'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1',
'X-Wf-1-Plugin-1' => 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3',
'X-Wf-1-1-1-1' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-2' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-1' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|',
'X-Wf-1-1-1-2' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|',
);
$expected2 = array(
'X-Wf-1-1-1-3' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-4' => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
'X-Wf-1-1-1-3' => '55|[{"Type":"LOG","File":"","Line":""},"test: test [] []"]|',
'X-Wf-1-1-1-4' => '56|[{"Type":"WARN","File":"","Line":""},"test: test [] []"]|',
);
$this->assertEquals($expected, $handler->getHeaders());

View File

@@ -20,6 +20,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
{
return array(
'message' => $message,
'context' => array(),
'level' => $level,
'level_name' => Logger::getLevelName($level),
'channel' => 'test',