-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Monolog 3.x (#5334)
* fix: Allow `ServiceException` creation with `null` as the message Some tests explicitly set the message to `null` which breaks the creation of parent exceptions with newer PHP versions. Setting a default `$message = ''` does not prevent the problem, so we declare it with `null` being allowed, and check and replace it in the `ServiceException` constructor. The change is made in the `ServiceException` instead of the tests calling it incorrectly to reduce the scope of updated files in the context of the PR that handles the problem. * feat: Enable the usage of `"psr/log": "^2.0|^3.0"` `phpdocumentor/reflection` started supporting `"psr/log": "^2.0|^3.0"` with v5.3. See phpDocumentor/Reflection#247 * feat: Enable the installation of Monolog V3 `psr/log` v1 included a TestLogger that was removed starting with v2. `fig/log-test` support all current versions of `psr/log`. * feat: Introduce V3 versions of AppEngine Formatters and Handlers * feat: Introduce LogMessageProcessors and their factory * add monolog 3 to root composer * remove provide * allow log-test 1.0 * remove log-test in favor of skipping tests * ignore cs warning Co-authored-by: Brent Shaffer <[email protected]>
- Loading branch information
1 parent
36cc50c
commit 6ad6bc3
Showing
22 changed files
with
559 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* 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\Core\Logger; | ||
|
||
use Monolog\Formatter\LineFormatter; | ||
use Monolog\LogRecord; | ||
|
||
/** | ||
* Monolog 3.x formatter for formatting logs on App Engine flexible environment. | ||
* | ||
* If you are using Monolog 1.x, use {@see \Google\Cloud\Core\Logger\AppEngineFlexFormatter} instead. | ||
* If you are using Monolog 2.x, use {@see \Google\Cloud\Core\Logger\AppEngineFlexFormatterV2} instead. | ||
*/ | ||
class AppEngineFlexFormatterV3 extends LineFormatter | ||
{ | ||
use FormatterTrait; | ||
|
||
/** | ||
* @param string $format [optional] The format of the message | ||
* @param string $dateFormat [optional] The format of the timestamp | ||
* @param bool $ignoreEmptyContextAndExtra [optional] | ||
*/ | ||
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 LogRecord $record A record to format | ||
* @return string The formatted record | ||
*/ | ||
public function format(LogRecord $record): string | ||
{ | ||
return $this->formatPayload($record, parent::format($record)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* 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\Core\Logger; | ||
|
||
use Monolog\Formatter\FormatterInterface; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
|
||
/** | ||
* Monolog 3.x handler for logging on App Engine flexible environment. | ||
* | ||
* If you are using Monolog 1.x, use {@see \Google\Cloud\Core\Logger\AppEngineFlexHandler} instead. | ||
* If you are using Monolog 2.x, use {@see \Google\Cloud\Core\Logger\AppEngineFlexHandlerV2} instead. | ||
*/ | ||
class AppEngineFlexHandlerV3 extends StreamHandler | ||
{ | ||
/** | ||
* @param int $level [optional] The minimum logging level at which this | ||
* handler will be triggered. | ||
* @param Boolean $bubble [optional] Whether the messages that are handled | ||
* can bubble up the stack or not. | ||
* @param int|null $filePermission [optional] Optional file permissions | ||
* (default (0640) are only for owner read/write). | ||
* @param Boolean $useLocking [optional] Try to lock log file before doing | ||
* any writes. | ||
* @param resource|string|null $stream [optional] | ||
*/ | ||
public function __construct( | ||
$level = Logger::INFO, | ||
$bubble = true, | ||
$filePermission = 0640, | ||
$useLocking = false, | ||
$stream = null | ||
) { | ||
if ($stream === null) { | ||
$pid = getmypid(); | ||
$stream = "file:///var/log/app_engine/app.$pid.json"; | ||
} | ||
parent::__construct( | ||
$stream, | ||
$level, | ||
$bubble, | ||
$filePermission, | ||
$useLocking | ||
); | ||
} | ||
|
||
protected function getDefaultFormatter(): FormatterInterface | ||
{ | ||
return new AppEngineFlexFormatterV3(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.