diff --git a/composer.json b/composer.json index 0d0d66c6b40b..61c6eb136b14 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "google/auth": "0.7", "guzzlehttp/guzzle": "~5.2|~6.0", "guzzlehttp/psr7": "1.2.*", + "monolog/monolog": "~1", "psr/http-message": "1.0.*" }, "require-dev": { diff --git a/src/Logger/AppEngineFlexFormatter.php b/src/Logger/AppEngineFlexFormatter.php new file mode 100644 index 000000000000..b4abcf7d905f --- /dev/null +++ b/src/Logger/AppEngineFlexFormatter.php @@ -0,0 +1,64 @@ + $message, + 'timestamp'=> ['seconds' => $sec, + 'nanos' => $usec], + 'thread' => '', + 'severity' => $record['level_name'], + ]; + if (isset($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'])) { + $payload['traceId'] = explode( + "/", + $_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'] + )[0]; + } + return "\n" . json_encode($payload); + } +} diff --git a/src/Logger/AppEngineFlexHandler.php b/src/Logger/AppEngineFlexHandler.php new file mode 100644 index 000000000000..73c048fa9a15 --- /dev/null +++ b/src/Logger/AppEngineFlexHandler.php @@ -0,0 +1,63 @@ +path = tempnam($dir, "log"); + $handler = new AppEngineFlexHandler( + Logger::DEBUG, true, 0640, false, $this->path + ); + $this->log = new Logger('gcloud-test'); + $this->log->pushHandler($handler); + } + + public function tearDown() + { + unlink($this->path); + } + + public function testOneLine() + { + $msg = 'Error message'; + $this->log->addError($msg); + $log_text = file_get_contents($this->path); + $log_array = json_decode($log_text, true); + $this->assertContains($msg, $log_array['message']); + $this->assertInternalType('int', $log_array['timestamp']['seconds']); + $this->assertInternalType('int', $log_array['timestamp']['nanos']); + $this->assertEquals('ERROR', $log_array['severity']); + } +}