1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 20:57:36 +02:00

Make $initialized static

Removed custom writer, overriding is easy enough
CS fixes
Added TestFirePHPHandler class to enable testing of headers
This commit is contained in:
Jordi Boggiano
2011-04-25 15:48:52 +02:00
parent a0b8f75b2b
commit 2b1c68e0d0
3 changed files with 83 additions and 138 deletions

View File

@@ -20,7 +20,6 @@ use Monolog\Logger;
*/
class WildfireFormatter extends LineFormatter implements FormatterInterface
{
/**
* Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
*/
@@ -48,7 +47,7 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
{
// Format record according with LineFormatter
$formatted = parent::format($record);
// Create JSON object describing the appearance of the message in the console
$json = json_encode(array(
array(
@@ -58,14 +57,14 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
),
$formatted['message'],
));
// The message itself is a serialization of the above JSON object + it's length
$formatted['message'] = sprintf(
'%s|%s|',
strlen($json),
$json
);
return $formatted;
}

View File

@@ -21,7 +21,6 @@ use Monolog\Formatter\WildfireFormatter;
*/
class FirePHPHandler extends AbstractHandler
{
/**
* WildFire JSON header message format
*/
@@ -37,59 +36,53 @@ class FirePHPHandler extends AbstractHandler
*/
const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
/**
* Whether or not Wildfire vendor-specific headers have been generated & sent yet
*/
private $initialized = false;
/**
* Header prefix for Wildfire to recognize & parse headers
*/
private $prefix = 'X-Wf';
const HEADER_PREFIX = 'X-Wf';
/**
* Whether or not Wildfire vendor-specific headers have been generated & sent yet
*/
protected static $initialized = false;
/**
* Shared static message index between potentially multiple handlers
* @var int
*/
private static $messageIndex = 1;
/**
* Function, Method or Closure for sending the header
*/
private $writer;
protected static $messageIndex = 1;
/**
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
* @param mixed $writer Function, Method or Closure to use for sending headers
*/
public function __construct($level = Logger::DEBUG, $bubble = false, $writer = null)
public function __construct($level = Logger::DEBUG, $bubble = false)
{
$this->level = $level;
$this->bubble = $bubble;
$this->writer = $writer;
}
/**
* Base header creation function used by init headers & record headers
*
* @var Array $meta Wildfire Plugin, Protocol & Structure Indexes
* @var String $message Log message
* @return String Complete header string ready for the client
* @param array $meta Wildfire Plugin, Protocol & Structure Indexes
* @param string $message Log message
* @return string Complete header string ready for the client
*/
protected function createHeader(Array $meta, $message)
protected function createHeader(array $meta, $message)
{
$header = sprintf('%s-%s', $this->prefix, join('-', $meta));
$header = sprintf('%s-%s', self::HEADER_PREFIX, join('-', $meta));
return array($header => $message);
}
/**
* Creates message header from record
*
*
* @see createHeader()
* @var Array $record
* @param array $record
*/
protected function createRecordHeader(Array $record)
protected function createRecordHeader(array $record)
{
// Wildfire is extensible to support multiple protocols & plugins in a single request,
// but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake.
@@ -124,21 +117,14 @@ class FirePHPHandler extends AbstractHandler
/**
* Send header string to the client
*
* @var String $header
* @var String $content
* @return Boolean False if headers are already sent, true if header are sent successfully
* @param string $header
* @param string $content
*/
protected function sendHeader($header, $content)
{
if (headers_sent()) {
return false;
} else if ($writer = $this->getWriter()) {
call_user_func_array($writer, array($header, $content));
} else {
if (!headers_sent()) {
header(sprintf('%s: %s', $header, $content));
}
return true;
}
/**
@@ -146,38 +132,20 @@ class FirePHPHandler extends AbstractHandler
*
* @see sendHeader()
* @see sendInitHeaders()
* @var Array $record
* @param array $record
*/
protected function write(Array $record)
protected function write(array $record)
{
// WildFire-specific headers must be sent prior to any messages
if (! $this->initialized) {
if (!self::$initialized) {
foreach ($this->getInitHeaders() as $header => $content) {
$this->sendHeader($header, $content);
}
$this->initialized = true;
self::$initialized = true;
}
foreach ($this->createRecordHeader($record) as $header => $content) {
$this->sendHeader($header, $content);
}
}
/**
* @return mixed Writer used for sending headers
*/
public function getWriter()
{
return $this->writer;
$header = $this->createRecordHeader($record);
$this->sendHeader(key($header), current($header));
}
/**
* @var mixed Function, Method or Closure to use for sending headers
*/
public function setWriter($writer)
{
$this->writer = $writer;
}
}

View File

@@ -16,95 +16,73 @@ use Monolog\Logger;
class FirePHPHandlerTest extends TestCase
{
/**
* @dataProvider handlerProvider
*/
public function testCloseReturnsHeadersSent($handler)
public function setUp()
{
$this->assertEquals(headers_sent(), $handler->close());
TestFirePHPHandler::reset();
}
/**
* @dataProvider handlerProvider
*/
public function testDefaultWriterIsNull($handler)
public function testHeaders()
{
$this->assertEquals(null, $handler->getWriter());
}
public function testConstructWithWriter()
{
$writer = array($this, 'testWriter');
$handler = new FirePHPHandler(Logger::DEBUG, false, $writer);
$this->assertEquals($writer, $handler->getWriter());
}
/**
* @dataProvider handlerProvider
*/
public function testWriterIsSettable($handler)
{
$writer = array($this, 'testWriter');
$handler->setWriter($writer);
$this->assertNotEquals('header', $handler->getWriter());
$this->assertEquals($writer, $handler->getWriter());
}
public function testMethodWriter()
{
$handler = new FirePHPHandler;
$handler->setWriter(array($this, 'writerForTestMethodWriter'));
$handler = new TestFirePHPHandler;
$handler->handle($this->getRecord(Logger::DEBUG));
}
$handler->handle($this->getRecord(Logger::WARNING));
public function writerForTestMethodWriter($header, $content)
{
$valid = array(
$expected = array(
'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/ZendFramework/FirePHP/1.6.2',
'X-Wf-1-1-1-5' => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
'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 "]|',
);
$this->assertTrue(array_key_exists($header, $valid));
$this->assertEquals($valid[$header], $content);
$this->assertEquals($expected, $handler->getHeaders());
}
public function testClosureWriter()
public function testConcurrentHandlers()
{
$headers = array();
$handler = new FirePHPHandler;
$handler->setWriter(function($header, $content) use (&$headers) {
$headers[$header] = $content;
});
$handler = new TestFirePHPHandler;
$handler->handle($this->getRecord(Logger::DEBUG));
$this->assertEquals(
'50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
end($headers)
);
$this->assertEquals(4, count($headers), "There should be 3 init headers & 1 message header");
}
public function handlerProvider()
{
$handler = new FirePHPHandler();
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::INFO));
$handler->handle($this->getRecord(Logger::WARNING));
return array(
array($handler),
$handler2 = new TestFirePHPHandler;
$handler2->handle($this->getRecord(Logger::DEBUG));
$handler2->handle($this->getRecord(Logger::WARNING));
$expected = array(
'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/ZendFramework/FirePHP/1.6.2',
'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 "]|',
);
$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 "]|',
);
$this->assertEquals($expected, $handler->getHeaders());
$this->assertEquals($expected2, $handler2->getHeaders());
}
}
class TestFirePHPHandler extends FirePHPHandler
{
protected $headers = array();
public static function reset()
{
self::$initialized = false;
self::$messageIndex = 1;
}
protected function sendHeader($header, $content)
{
$this->headers[$header] = $content;
}
public function getHeaders()
{
return $this->headers;
}
}