From 15ead0bad847d6d57e7697ca03b98bb097891ab2 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Tue, 19 Nov 2024 12:01:10 +0000 Subject: [PATCH] add StdOutLogger --- src/BasicGoogleCloudLogger.php | 20 +----- src/StdOutLogger.php | 39 +++++++++++ tests/StdOutLoggerTest.php | 117 +++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 19 deletions(-) create mode 100644 src/StdOutLogger.php create mode 100644 tests/StdOutLoggerTest.php diff --git a/src/BasicGoogleCloudLogger.php b/src/BasicGoogleCloudLogger.php index 46da42b..130ba81 100644 --- a/src/BasicGoogleCloudLogger.php +++ b/src/BasicGoogleCloudLogger.php @@ -7,26 +7,8 @@ /** * Logger that outputs the messages using error_log() */ -class BasicGoogleCloudLogger extends ErrorLogLogger +class BasicGoogleCloudLogger extends StdOutLogger { - private $_handle; - - public function __construct($maxLevel = LogLevel::DEBUG) - { - parent::__construct($maxLevel); - $this->_handle = fopen('php://stderr', 'wb'); - } - - public function setHandle($handle) - { - $this->_handle = $handle; - } - - protected function _writeLog($message) - { - fwrite($this->_handle, $message); - } - protected function _formatLog($level, $message, array $context = null) { return json_encode(array_filter([ diff --git a/src/StdOutLogger.php b/src/StdOutLogger.php new file mode 100644 index 0000000..dda07cd --- /dev/null +++ b/src/StdOutLogger.php @@ -0,0 +1,39 @@ +setHandle(STDOUT); + } + + public function setHandle($handle) + { + $this->_handle = $handle; + } + + protected function _writeLog($message) + { + fwrite($this->_handle, $message); + } + + protected function _formatLog($level, $message, array $context = null) + { + if(!empty($context)) + { + $message .= ' ' . json_encode($context); + } + $ulevel = strtoupper($level); + return "[{$ulevel}] $message" . PHP_EOL; + } +} diff --git a/tests/StdOutLoggerTest.php b/tests/StdOutLoggerTest.php new file mode 100644 index 0000000..70a4fc0 --- /dev/null +++ b/tests/StdOutLoggerTest.php @@ -0,0 +1,117 @@ +_tempFile = tempnam(sys_get_temp_dir(), 'packaged-log-'); + $this->_handler = fopen($this->_tempFile, 'wb'); + } + + public function tearDown() + { + fclose($this->_handler); + unlink($this->_tempFile); + } + + protected function _getLogContents() + { + return file_get_contents($this->_tempFile); + } + + public function assertLastLog($test) + { + self::assertStringEndsWith($test . PHP_EOL, $this->_getLogContents()); + } + + private function _getTestLogger($maxLevel = LogLevel::DEBUG) + { + $l = new StdOutLogger($maxLevel); + $l->setHandle($this->_handler); + return $l; + } + + public function testErrorLogLogger() + { + Log::bind($this->_getTestLogger()); + + Log::debug('debug: test'); + self::assertLastLog('debug: test'); + + Log::info('info: test'); + self::assertLastLog('info: test'); + + Log::notice('notice: test'); + self::assertLastLog('notice: test'); + + Log::warning('warning: test'); + self::assertLastLog('warning: test'); + + Log::error('error: test'); + self::assertLastLog('error: test'); + + Log::critical('critical: test'); + self::assertLastLog('critical: test'); + + Log::alert('alert: test'); + self::assertLastLog('alert: test'); + + Log::emergency('emergency: test'); + self::assertLastLog('emergency: test'); + } + + public function testLevelLog() + { + Log::bind($this->_getTestLogger(LogLevel::INFO)); + + Log::info('info: test'); + self::assertLastLog('info: test'); + + Log::debug('debug: test'); + self::assertLastLog('info: test'); + } + + public function testExceptionLog() + { + Log::bind($this->_getTestLogger()); + + $e = new Exception('exception message', 123); + Log::exception($e); + self::assertContains('[CRITICAL] exception message ', $this->_getLogContents()); + self::assertContains('"code":123', $this->_getLogContents()); + self::assertContains('"line":90', $this->_getLogContents()); + self::assertContains('StdOutLoggerTest.php', $this->_getLogContents()); + } + + public function testExceptionTraceLog() + { + Log::bind($this->_getTestLogger()); + + $e = new Exception('exception message', 123); + Log::exceptionWithTrace($e); + self::assertContains('[CRITICAL] exception message ', $this->_getLogContents()); + self::assertContains('"code":123', $this->_getLogContents()); + self::assertContains('"line":102', $this->_getLogContents()); + self::assertContains('StdOutLoggerTest.php', $this->_getLogContents()); + self::assertContains('"stack_trace"', $this->_getLogContents()); + } + + public function testContextLog() + { + Log::bind($this->_getTestLogger()); + Log::debug('debug: test', ['test1' => 'value1', 'test2' => 'value2']); + self::assertLastLog('debug: test {"test1":"value1","test2":"value2"}'); + } +}