瀏覽代碼

Change timezone to be per-instance and not global

Jordi Boggiano 10 年之前
父節點
當前提交
6505e02fd3
共有 3 個文件被更改,包括 29 次插入18 次删除
  1. 5 0
      UPGRADE.md
  2. 23 17
      src/Monolog/Logger.php
  3. 1 1
      tests/Monolog/LoggerTest.php

+ 5 - 0
UPGRADE.md

@@ -0,0 +1,5 @@
+### 2.0.0
+
+- The timezone is now set per Logger instance and not statically, either
+  via ->setTimezone or passed in the constructor. Calls to Logger::setTimezone
+  should be converted.

+ 23 - 17
src/Monolog/Logger.php

@@ -102,11 +102,6 @@ class Logger implements LoggerInterface
         self::EMERGENCY => 'EMERGENCY',
     );
 
-    /**
-     * @var \DateTimeZone
-     */
-    protected static $timezone;
-
     /**
      * @var string
      */
@@ -133,16 +128,23 @@ class Logger implements LoggerInterface
      */
     protected $microsecondTimestamps = true;
 
+    /**
+     * @var \DateTimeZone
+     */
+    protected $timezone;
+
     /**
      * @param string             $name       The logging channel
      * @param HandlerInterface[] $handlers   Optional stack of handlers, the first one in the array is called first, etc.
      * @param callable[]         $processors Optional array of processors
+     * @param DateTimeZone       $timezone   Optional timezone, if not provided date_default_timezone_get() will be used
      */
-    public function __construct($name, array $handlers = array(), array $processors = array())
+    public function __construct($name, array $handlers = array(), array $processors = array(), $timezone = null)
     {
         $this->name = $name;
         $this->handlers = $handlers;
         $this->processors = $processors;
+        $this->timezone = new \DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC');
     }
 
     /**
@@ -294,16 +296,12 @@ class Logger implements LoggerInterface
             return false;
         }
 
-        if (!static::$timezone) {
-            static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');
-        }
-
         if ($this->microsecondTimestamps) {
-            $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone);
+            $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $this->timezone);
         } else {
-            $ts = new \DateTime(null, static::$timezone);
+            $ts = new \DateTime(null, $this->timezone);
         }
-        $ts->setTimezone(static::$timezone);
+        $ts->setTimezone($this->timezone);
 
         $record = array(
             'message' => (string) $message,
@@ -675,12 +673,20 @@ class Logger implements LoggerInterface
     /**
      * Set the timezone to be used for the timestamp of log records.
      *
-     * This is stored globally for all Logger instances
-     *
      * @param \DateTimeZone $tz Timezone object
      */
-    public static function setTimezone(\DateTimeZone $tz)
+    public function setTimezone(\DateTimeZone $tz)
+    {
+        $this->timezone = $tz;
+    }
+
+    /**
+     * Set the timezone to be used for the timestamp of log records.
+     *
+     * @return \DateTimeZone
+     */
+    public function getTimezone()
     {
-        self::$timezone = $tz;
+        return $this->timezone;
     }
 }

+ 1 - 1
tests/Monolog/LoggerTest.php

@@ -491,8 +491,8 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
      */
     public function testSetTimezone($tz)
     {
-        Logger::setTimezone($tz);
         $logger = new Logger('foo');
+        $logger->setTimezone($tz);
         $handler = new TestHandler;
         $logger->pushHandler($handler);
         $logger->info('test');