From 169650dc5ba6ec6a3f9bf465242ca8d1059082bb Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Feb 2021 21:50:06 +0000 Subject: [PATCH] MainLogger: accept timezone as a constructor parameter this makes it easier to unit-test, as well as making it independent of Timezone. --- src/PocketMine.php | 2 +- src/utils/MainLogger.php | 4 ++-- tests/phpunit/scheduler/AsyncPoolTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PocketMine.php b/src/PocketMine.php index 160204d35..5740b20cf 100644 --- a/src/PocketMine.php +++ b/src/PocketMine.php @@ -237,7 +237,7 @@ namespace pocketmine { Terminal::init(); } - $logger = new MainLogger($dataPath . "server.log", Terminal::hasFormattingCodes(), "Server"); + $logger = new MainLogger($dataPath . "server.log", Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get())); \GlobalLogger::set($logger); emit_performance_warnings($logger); diff --git a/src/utils/MainLogger.php b/src/utils/MainLogger.php index f5637dbe9..f2c1ae160 100644 --- a/src/utils/MainLogger.php +++ b/src/utils/MainLogger.php @@ -55,13 +55,13 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{ /** * @throws \RuntimeException */ - public function __construct(string $logFile, bool $useFormattingCodes, string $mainThreadName, bool $logDebug = false){ + public function __construct(string $logFile, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){ parent::__construct(); $this->logDebug = $logDebug; $this->useFormattingCodes = $useFormattingCodes; $this->mainThreadName = $mainThreadName; - $this->timezone = Timezone::get(); + $this->timezone = $timezone->getName(); $this->logWriterThread = new MainLoggerThread($logFile); $this->logWriterThread->start(PTHREADS_INHERIT_NONE); diff --git a/tests/phpunit/scheduler/AsyncPoolTest.php b/tests/phpunit/scheduler/AsyncPoolTest.php index 9cba443a0..a152c3ec3 100644 --- a/tests/phpunit/scheduler/AsyncPoolTest.php +++ b/tests/phpunit/scheduler/AsyncPoolTest.php @@ -43,7 +43,7 @@ class AsyncPoolTest extends TestCase{ public function setUp() : void{ @define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); - $this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog"), false, "Main"); + $this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog"), false, "Main", new \DateTimeZone('UTC')); $this->pool = new AsyncPool(2, 1024, new \BaseClassLoader(), $this->mainLogger, new SleeperHandler()); }