possible to use a concrete layout
authorFlorian Semm <floriansemm@apache.org>
Mon, 26 Mar 2012 05:21:59 +0000 (05:21 +0000)
committerFlorian Semm <floriansemm@apache.org>
Mon, 26 Mar 2012 05:21:59 +0000 (05:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/logging/log4php/trunk@1305226 13f79535-47bb-0310-9956-ffa450edef68

src/main/php/appenders/LoggerAppenderFirephp.php
src/test/php/appenders/LoggerAppenderFirephpTest.php

index 16f870e..afb6346 100644 (file)
@@ -57,8 +57,6 @@ class LoggerAppenderFirephp extends LoggerAppender {
         * @param string $name Default ''
         */
        public function __construct($name = '') {
         * @param string $name Default ''
         */
        public function __construct($name = '') {
-               $this->requiresLayout = false;
-               
                parent::__construct($name);
        }
 
                parent::__construct($name);
        }
 
@@ -81,12 +79,12 @@ class LoggerAppenderFirephp extends LoggerAppender {
        public function append(LoggerLoggingEvent $event) {
                $console = $this->getConsole();
                if (null === $console) {
        public function append(LoggerLoggingEvent $event) {
                $console = $this->getConsole();
                if (null === $console) {
-                       return;         //Silently fail, if FirePHP is unavailable
+                       $this->warn('FirePHP is not installed correctly.');
                }
 
                }
 
-               $msg = $this->formatMessage($event);
-
-               switch (strtolower($event->getLevel()->toString())) {
+               $msg = $this->getLayout()->format($event);
+               
+               switch ($this->getLogLevel($event)) {
                case 'debug':
                        $console->trace($msg);  //includes backtrace
                        break;
                case 'debug':
                        $console->trace($msg);  //includes backtrace
                        break;
@@ -103,11 +101,11 @@ class LoggerAppenderFirephp extends LoggerAppender {
                        $console->info($msg);
                }
        }
                        $console->info($msg);
                }
        }
-
-       private function formatMessage(LoggerLoggingEvent $event) {
-               return '[' . $event->getLevel()->toString() . '] - ' . $event->getRenderedMessage();
-       }
        
        
+       private function getLogLevel(LoggerLoggingEvent $event) {
+               return strtolower($event->getLevel()->toString());
+       }
+
        /**
         * Disable
         *
        /**
         * Disable
         *
index 4e783f4..96a10c2 100644 (file)
@@ -27,7 +27,7 @@
 require_once('FirePHPCore/FirePHP.class.php');
 
 /**
 require_once('FirePHPCore/FirePHP.class.php');
 
 /**
- * @group firephp
+ * @group appenders
  */
 class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
 
  */
 class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
 
@@ -46,35 +46,18 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
                )
        );
 
                )
        );
 
-       public function testRequiresLayout() {
-               $appender = new LoggerAppenderFirephp();
-               self::assertFalse($appender->requiresLayout());
-       }
-
+       private function createEvent($message, $level) {\r
+               $eventMock = new LoggerLoggingEvent("LoggerAppenderFirephpTest", new Logger("TEST"), LoggerLevel::toLevel($level), $message);\r
+       \r
+               return $eventMock;\r
+       }       
+       
        public function testSetMedium() {
                $appender = new LoggerAppenderFirephp();
                $appender->setMedium('page');
                self::assertSame('page', $appender->getMedium());
        }
 
        public function testSetMedium() {
                $appender = new LoggerAppenderFirephp();
                $appender->setMedium('page');
                self::assertSame('page', $appender->getMedium());
        }
 
-       private function createEvent($message, $level) {
-               $eventMock = $this->getMock('LoggerLoggingEvent', array(), array(), '', false);
-               $eventMock->expects($this->any())
-                                 ->method('getRenderedMessage')
-                                 ->will($this->returnValue($message));
-               
-               $levelMock = $this->getMock('LoggerLevel', array(), array(), '', false);
-               $levelMock->expects($this->any())
-                                 ->method('toString')
-                                 ->will($this->returnValue($level));
-               
-               $eventMock->expects($this->any())\r
-                                 ->method('getLevel')\r
-                                 ->will($this->returnValue($levelMock));
-               
-               return $eventMock;
-       }
-       
        public function testAppend_HandleDebug() {
                $console = new FirePHPSpy();
                
        public function testAppend_HandleDebug() {
                $console = new FirePHPSpy();
                
@@ -86,7 +69,7 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
                
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));
                
                
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));
                
-               $this->assertLog($console, $expectedMessage, 'debug', 'trace');
+               $this->assertLog($console, $expectedMessage, $expectedLevel, 'trace');
        }
        
        public function testAppend_HandleWarn() {\r
        }
        
        public function testAppend_HandleWarn() {\r
@@ -100,7 +83,7 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
                
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
                
-               $this->assertLog($console, $expectedMessage, 'warn', 'debug');\r
+               $this->assertLog($console, $expectedMessage, $expectedLevel, 'debug');\r
        }
        
        public function testAppend_HandleError() {\r
        }
        
        public function testAppend_HandleError() {\r
@@ -114,7 +97,7 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));
                \r
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));
                \r
-               $this->assertLog($console, $expectedMessage, 'error', 'warn');\r
+               $this->assertLog($console, $expectedMessage, $expectedLevel, 'warn');\r
        }       
        
        public function testAppend_HandleFatal() {\r
        }       
        
        public function testAppend_HandleFatal() {\r
@@ -123,12 +106,12 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
                $appender = new TestableLoggerAppenderFirePhp();\r
                $appender->setConsole($console);\r
        \r
                $appender = new TestableLoggerAppenderFirePhp();\r
                $appender->setConsole($console);\r
        \r
-               $expectedMessage = 'fatal message';\r
+               $expectedMessage = "fatal message";\r
                $expectedLevel = 'fatal';\r
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
 
                $expectedLevel = 'fatal';\r
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
 
-               $this->assertLog($console, $expectedMessage, 'fatal', 'error');\r
+               $this->assertLog($console, $expectedMessage, $expectedLevel, 'error');\r
        }
        
        public function testAppend_HandleDefault() {\r
        }
        
        public function testAppend_HandleDefault() {\r
@@ -142,13 +125,18 @@ class LoggerAppenderFirephpTest extends PHPUnit_Framework_TestCase {
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
        
        \r
                $appender->append($this->createEvent($expectedMessage, $expectedLevel));\r
        
-               $this->assertLog($console, $expectedMessage, 'info', 'info');
+               $this->assertLog($console, $expectedMessage, $expectedLevel, 'info');
        }
        
        public function assertLog($console, $expectedMessage, $logLevel, $calledMethod) {
        }
        
        public function assertLog($console, $expectedMessage, $logLevel, $calledMethod) {
-               $this->assertEquals('['.$logLevel.']'.' - '.$expectedMessage, $console->getMessage());\r
+               $event = $this->createEvent($expectedMessage, $logLevel);\r
+               \r
+               $layout = new LoggerLayoutSimple();\r
+               $message = $layout->format($event);
+               
+               $this->assertEquals($message, $console->getMessage(), 'log message is wrong');\r
                $this->assertEquals(1, $console->getCalls(), 'wasn\'t called once');
                $this->assertEquals(1, $console->getCalls(), 'wasn\'t called once');
-               $this->assertEquals($calledMethod, $console->getCalledMethod());
+               $this->assertEquals($calledMethod, $console->getCalledMethod(), 'wrong log-method was called');
        }
 }
 
        }
 }