1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +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 class WildfireFormatter extends LineFormatter implements FormatterInterface
{ {
/** /**
* Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]" * Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
*/ */
@@ -48,7 +47,7 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
{ {
// Format record according with LineFormatter // Format record according with LineFormatter
$formatted = parent::format($record); $formatted = parent::format($record);
// Create JSON object describing the appearance of the message in the console // Create JSON object describing the appearance of the message in the console
$json = json_encode(array( $json = json_encode(array(
array( array(
@@ -58,14 +57,14 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
), ),
$formatted['message'], $formatted['message'],
)); ));
// The message itself is a serialization of the above JSON object + it's length // The message itself is a serialization of the above JSON object + it's length
$formatted['message'] = sprintf( $formatted['message'] = sprintf(
'%s|%s|', '%s|%s|',
strlen($json), strlen($json),
$json $json
); );
return $formatted; return $formatted;
} }

View File

@@ -21,7 +21,6 @@ use Monolog\Formatter\WildfireFormatter;
*/ */
class FirePHPHandler extends AbstractHandler class FirePHPHandler extends AbstractHandler
{ {
/** /**
* WildFire JSON header message format * 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'; 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 * 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 * Shared static message index between potentially multiple handlers
* @var int
*/ */
private static $messageIndex = 1; protected static $messageIndex = 1;
/**
* Function, Method or Closure for sending the header
*/
private $writer;
/** /**
* @param integer $level The minimum logging level at which this handler will be triggered * @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 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->level = $level;
$this->bubble = $bubble; $this->bubble = $bubble;
$this->writer = $writer;
} }
/** /**
* Base header creation function used by init headers & record headers * Base header creation function used by init headers & record headers
* *
* @var Array $meta Wildfire Plugin, Protocol & Structure Indexes * @param array $meta Wildfire Plugin, Protocol & Structure Indexes
* @var String $message Log message * @param string $message Log message
* @return String Complete header string ready for the client * @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); return array($header => $message);
} }
/** /**
* Creates message header from record * Creates message header from record
* *
* @see createHeader() * @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, // 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. // 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 * Send header string to the client
* *
* @var String $header * @param string $header
* @var String $content * @param string $content
* @return Boolean False if headers are already sent, true if header are sent successfully
*/ */
protected function sendHeader($header, $content) protected function sendHeader($header, $content)
{ {
if (headers_sent()) { if (!headers_sent()) {
return false;
} else if ($writer = $this->getWriter()) {
call_user_func_array($writer, array($header, $content));
} else {
header(sprintf('%s: %s', $header, $content)); header(sprintf('%s: %s', $header, $content));
} }
return true;
} }
/** /**
@@ -146,38 +132,20 @@ class FirePHPHandler extends AbstractHandler
* *
* @see sendHeader() * @see sendHeader()
* @see sendInitHeaders() * @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 // WildFire-specific headers must be sent prior to any messages
if (! $this->initialized) { if (!self::$initialized) {
foreach ($this->getInitHeaders() as $header => $content) { foreach ($this->getInitHeaders() as $header => $content) {
$this->sendHeader($header, $content); $this->sendHeader($header, $content);
} }
$this->initialized = true; self::$initialized = true;
} }
foreach ($this->createRecordHeader($record) as $header => $content) {
$this->sendHeader($header, $content);
}
}
/** $header = $this->createRecordHeader($record);
* @return mixed Writer used for sending headers $this->sendHeader(key($header), current($header));
*/
public function getWriter()
{
return $this->writer;
} }
/**
* @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 class FirePHPHandlerTest extends TestCase
{ {
public function setUp()
/**
* @dataProvider handlerProvider
*/
public function testCloseReturnsHeadersSent($handler)
{ {
$this->assertEquals(headers_sent(), $handler->close()); TestFirePHPHandler::reset();
} }
/** public function testHeaders()
* @dataProvider handlerProvider
*/
public function testDefaultWriterIsNull($handler)
{ {
$this->assertEquals(null, $handler->getWriter()); $handler = new TestFirePHPHandler;
}
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->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::DEBUG));
} $handler->handle($this->getRecord(Logger::WARNING));
public function writerForTestMethodWriter($header, $content) $expected = array(
{
$valid = array(
'X-Wf-Protocol-1' => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2', '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-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-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($expected, $handler->getHeaders());
$this->assertEquals($valid[$header], $content);
} }
public function testClosureWriter() public function testConcurrentHandlers()
{ {
$headers = array(); $handler = new TestFirePHPHandler;
$handler = new FirePHPHandler;
$handler->setWriter(function($header, $content) use (&$headers) {
$headers[$header] = $content;
});
$handler->handle($this->getRecord(Logger::DEBUG)); $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)); $handler->handle($this->getRecord(Logger::WARNING));
return array( $handler2 = new TestFirePHPHandler;
array($handler), $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;
}
}