From 5d7db08106dc276b6693b64a4a87428ab8c62779 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Wed, 20 May 2015 22:05:03 -0600 Subject: [PATCH] Add Logger::setTimezone() Add a setter method to allow changing the timezone used when creating new log records. This provides a means to correct a behavioral change introduced by 6cbdc04 where use of the PHP runtime default timezone provided by `date_default_timezone_get()` was introduced. In most environments the new default behavior is preferable, but if an application allows the default timezone to vary between requests it causes confusing log output. --- src/Monolog/Logger.php | 10 ++++++++++ tests/Monolog/LoggerTest.php | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 48495a33..af6b72b3 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -614,4 +614,14 @@ class Logger implements LoggerInterface { return $this->addRecord(static::EMERGENCY, $message, $context); } + + /** + * Set the timezone to be used for the timestamp of log records. + * + * @param string $tz Timezone name + */ + public static function setTimezone($tz) + { + self::$timezone = new \DateTimeZone($tz); + } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index d5bb657f..83f17894 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -421,4 +421,27 @@ class LoggerTest extends \PHPUnit_Framework_TestCase array('emerg', Logger::EMERGENCY), ); } + + /** + * @dataProvider setTimezoneProvider + * @covers Monolog\Logger::setTimezone + */ + public function testSetTimezone($tz) + { + Logger::setTimezone($tz); + $logger = new Logger('foo'); + $handler = new TestHandler; + $logger->pushHandler($handler); + $logger->info('test'); + list($record) = $handler->getRecords(); + $this->assertEquals($tz, $record['datetime']->getTimezone()->getName()); + } + + public function setTimezoneProvider() + { + return array_map( + function ($tz) { return array($tz); }, + \DateTimeZone::listIdentifiers() + ); + } }