-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added loggers for App Engine flexible environment. #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace Google\Cloud\Logger; | ||
|
||
use Monolog\Formatter\LineFormatter; | ||
|
||
/** | ||
* Class for formatting logs on App Engine flexible environment. | ||
*/ | ||
class AppEngineFlexFormatter extends LineFormatter | ||
{ | ||
/** | ||
* @param string $format The format of the message | ||
* @param string $dateFormat The format of the timestamp | ||
* @param bool $ignoreEmptyContextAndExtra | ||
*/ | ||
public function __construct($format = null, $dateFormat = null, $ignoreEmptyContextAndExtra = false) | ||
{ | ||
parent::__construct($format, $dateFormat, true, $ignoreEmptyContextAndExtra); | ||
} | ||
|
||
/** | ||
* Get the plain text message with LineFormatter's format method and add | ||
* metadata including the trace id then return the json string. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
$message = parent::format($record); | ||
list($usec, $sec) = explode(" ", microtime()); | ||
$usec = (int)(((float)$usec)*1000000000); | ||
$sec = (int)$sec; | ||
$payload = [ | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
'message' => $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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace Google\Cloud\Logger; | ||
|
||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
|
||
/** | ||
* Class for logging on App Engine flexible environment. | ||
*/ | ||
class AppEngineFlexHandler extends StreamHandler | ||
{ | ||
/** | ||
* @param int $level The minimum logging level at which this handler will be triggered. | ||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not. | ||
* @param int|null $filePermission Optional file permissions (default (0640) are only for owner read/write). | ||
* @param Boolean $useLocking Try to lock log file before doing any writes. | ||
* @param resource|string|null $stream | ||
*/ | ||
public function __construct( | ||
$level = Logger::INFO, | ||
$bubble = true, | ||
$filePermission = 0640, | ||
$useLocking = false, | ||
$stream = null | ||
) { | ||
if ($stream === null) { | ||
$pid = posix_getpid(); | ||
$stream = "file:///var/log/app_engine/app.$pid.json"; | ||
} | ||
parent::__construct( | ||
$stream, | ||
$level, | ||
$bubble, | ||
$filePermission, | ||
$useLocking | ||
); | ||
} | ||
|
||
/** | ||
* Get the default formatter. | ||
* | ||
* @return FormatterInterface | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
*/ | ||
protected function getDefaultFormatter() | ||
{ | ||
return new AppEngineFlexFormatter(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright 2016 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Tests\Logger; | ||
|
||
use Google\Cloud\Logger\AppEngineFlexHandler; | ||
use Monolog\Logger; | ||
|
||
class AppEngineFlexHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $path; | ||
private $log; | ||
|
||
public function setUp() | ||
{ | ||
$dir = sys_get_temp_dir(); | ||
$this->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']); | ||
} | ||
} |
This comment was marked as spam.
Sorry, something went wrong.