Sfoglia il codice sorgente

[ZF-9870] Zend_Log - line 303 - Allow us to define timestamp

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22267 44c647ce-9c0f-0410-b52a-842ac1e357ba
jan 15 anni fa
parent
commit
56074b7e83
2 ha cambiato i file con 53 aggiunte e 1 eliminazioni
  1. 29 1
      library/Zend/Log.php
  2. 24 0
      tests/Zend/Log/LogTest.php

+ 29 - 1
library/Zend/Log.php

@@ -87,6 +87,12 @@ class Zend_Log
      * @var array
      */
     protected $_errorHandlerMap        = false;
+    
+    /**
+     *
+     * @var string
+     */
+    protected $_timestampFormat        = 'c';
 
     /**
      * Class constructor.  Create a new logger
@@ -253,7 +259,7 @@ class Zend_Log
     protected function _packEvent($message, $priority)
     {
         return array_merge(array(
-            'timestamp'    => date('c'),
+            'timestamp'    => date($this->_timestampFormat),
             'message'      => $message,
             'priority'     => $priority,
             'priorityName' => $this->_priorities[$priority]
@@ -534,4 +540,26 @@ class Zend_Log
         }
         return false;
     }
+
+    /**
+     * Set timestamp format for log entries.
+     * 
+     * @param string $format
+     * @return Zend_Log
+     */
+    public function setTimestampFormat($format)
+    {
+        $this->_timestampFormat = $format;
+        return $this;
+    }
+
+    /**
+     * Get timestamp format used for log entries.
+     * 
+     * @return string
+     */
+    public function getTimestampFormat()
+    {
+        return $this->_timestampFormat;
+    }
 }

+ 24 - 0
tests/Zend/Log/LogTest.php

@@ -343,4 +343,28 @@ class Zend_Log_LogTest extends PHPUnit_Framework_TestCase
             $this->assertTrue(empty($this->errWriter->events));
         }
     }
+    
+    /**
+     * @group ZF-9870
+     */
+    public function testSetAndGetTimestampFormat()
+    {
+        $logger = new Zend_Log($this->writer);
+        $this->assertEquals('c', $logger->getTimestampFormat());
+        $this->assertSame($logger, $logger->setTimestampFormat('Y-m-d H:i:s'));
+        $this->assertEquals('Y-m-d H:i:s', $logger->getTimestampFormat());
+    }
+    
+    /**
+     * @group ZF-9870
+     */
+    public function testLogWritesWithModifiedTimestampFormat()
+    {
+        $logger = new Zend_Log($this->writer);
+        $logger->setTimestampFormat('Y-m-d');
+        $logger->debug('ZF-9870');
+        rewind($this->log);
+        $message = stream_get_contents($this->log);
+        $this->assertEquals(date('Y-m-d'), substr($message, 0, 10));
+    }
 }