1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 13:46:38 +02:00

Change timezone to be per-instance and not global

This commit is contained in:
Jordi Boggiano
2015-10-09 19:07:08 +01:00
parent d381b20b87
commit 6505e02fd3
3 changed files with 29 additions and 18 deletions

5
UPGRADE.md Normal file
View File

@@ -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.

View File

@@ -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)
{
self::$timezone = $tz;
$this->timezone = $tz;
}
/**
* Set the timezone to be used for the timestamp of log records.
*
* @return \DateTimeZone
*/
public function getTimezone()
{
return $this->timezone;
}
}

View File

@@ -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');