mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-08 14:16:42 +02:00
Change timezone to be per-instance and not global
This commit is contained in:
5
UPGRADE.md
Normal file
5
UPGRADE.md
Normal 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.
|
@@ -102,11 +102,6 @@ class Logger implements LoggerInterface
|
|||||||
self::EMERGENCY => 'EMERGENCY',
|
self::EMERGENCY => 'EMERGENCY',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \DateTimeZone
|
|
||||||
*/
|
|
||||||
protected static $timezone;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@@ -133,16 +128,23 @@ class Logger implements LoggerInterface
|
|||||||
*/
|
*/
|
||||||
protected $microsecondTimestamps = true;
|
protected $microsecondTimestamps = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \DateTimeZone
|
||||||
|
*/
|
||||||
|
protected $timezone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name The logging channel
|
* @param string $name The logging channel
|
||||||
* @param HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first, etc.
|
* @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 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->name = $name;
|
||||||
$this->handlers = $handlers;
|
$this->handlers = $handlers;
|
||||||
$this->processors = $processors;
|
$this->processors = $processors;
|
||||||
|
$this->timezone = new \DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,16 +296,12 @@ class Logger implements LoggerInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!static::$timezone) {
|
|
||||||
static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->microsecondTimestamps) {
|
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 {
|
} else {
|
||||||
$ts = new \DateTime(null, static::$timezone);
|
$ts = new \DateTime(null, $this->timezone);
|
||||||
}
|
}
|
||||||
$ts->setTimezone(static::$timezone);
|
$ts->setTimezone($this->timezone);
|
||||||
|
|
||||||
$record = array(
|
$record = array(
|
||||||
'message' => (string) $message,
|
'message' => (string) $message,
|
||||||
@@ -675,12 +673,20 @@ class Logger implements LoggerInterface
|
|||||||
/**
|
/**
|
||||||
* Set the timezone to be used for the timestamp of log records.
|
* 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
|
* @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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -491,8 +491,8 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testSetTimezone($tz)
|
public function testSetTimezone($tz)
|
||||||
{
|
{
|
||||||
Logger::setTimezone($tz);
|
|
||||||
$logger = new Logger('foo');
|
$logger = new Logger('foo');
|
||||||
|
$logger->setTimezone($tz);
|
||||||
$handler = new TestHandler;
|
$handler = new TestHandler;
|
||||||
$logger->pushHandler($handler);
|
$logger->pushHandler($handler);
|
||||||
$logger->info('test');
|
$logger->info('test');
|
||||||
|
Reference in New Issue
Block a user