-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce LogMessageProcessors and their factory
- Loading branch information
1 parent
eeb4272
commit bf29fc7
Showing
6 changed files
with
202 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 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\Logging; | ||
|
||
/** | ||
* Interface representing a Message Processor used by the {@see PsrLogger} | ||
*/ | ||
interface LogMessageProcessor | ||
{ | ||
/** | ||
* @param string|\Stringable $message | ||
* @param array $context | ||
* | ||
* @phpstan-return array{message: string, context: array} | ||
* @return array | ||
*/ | ||
public function processLogMessage($message, $context); | ||
} |
55 changes: 55 additions & 0 deletions
55
Logging/src/LogMessageProcessor/MonologMessageProcessor.php
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,55 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 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\Logging\LogMessageProcessor; | ||
|
||
use Google\Cloud\Logging\LogMessageProcessor; | ||
use Monolog\Formatter\NormalizerFormatter; | ||
use Monolog\Processor\PsrLogMessageProcessor; | ||
|
||
/** | ||
* Uses the Monolog 1/2 PsrLogMessageProcessor to process a | ||
* record's message according to PSR-3 rules. | ||
*/ | ||
final class MonologMessageProcessor implements LogMessageProcessor | ||
{ | ||
/** | ||
* @var NormalizerFormatter | ||
*/ | ||
private $formatter; | ||
|
||
/** | ||
* @var PsrLogMessageProcessor | ||
*/ | ||
private $processor; | ||
|
||
public function __construct() | ||
{ | ||
$this->formatter = new NormalizerFormatter(); | ||
$this->processor = new PsrLogMessageProcessor(); | ||
} | ||
|
||
public function processLogMessage($message, $context) | ||
{ | ||
$processor = $this->processor; | ||
|
||
return $processor([ | ||
'message' => (string) $message, | ||
'context' => $this->formatter->format($context) | ||
]); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php
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,60 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 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\Logging\LogMessageProcessor; | ||
|
||
use Google\Cloud\Logging\LogMessageProcessor; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use Monolog\Processor\PsrLogMessageProcessor; | ||
|
||
/** | ||
* Uses the Monolog 3 PsrLogMessageProcessor to process a | ||
* record's message according to PSR-3 rules. | ||
*/ | ||
final class MonologV3MessageProcessor implements LogMessageProcessor | ||
{ | ||
/** | ||
* @var PsrLogMessageProcessor | ||
*/ | ||
private $processor; | ||
|
||
public function __construct() | ||
{ | ||
$this->processor = new PsrLogMessageProcessor(); | ||
} | ||
|
||
public function processLogMessage($message, $context) | ||
{ | ||
// The datetime, channel, and level are required but not relevant here | ||
$logRecord = new LogRecord( | ||
new \DateTimeImmutable(), | ||
'channel', | ||
Level::Info, | ||
(string) $message, | ||
$context | ||
); | ||
|
||
$processor = $this->processor; | ||
$processed = $processor($logRecord); | ||
|
||
return [ | ||
'message' => $processed['message'], | ||
'context' => $processed['context'], | ||
]; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 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\Logging; | ||
|
||
use Google\Cloud\Logging\LogMessageProcessor\MonologMessageProcessor; | ||
use Google\Cloud\Logging\LogMessageProcessor\MonologV3MessageProcessor; | ||
use Monolog\Logger as MonologLogger; | ||
use RuntimeException; | ||
|
||
/** | ||
* Returns a LogMessageProcessor that can be used with the currently install Monolog version | ||
*/ | ||
class LogMessageProcessorFactory | ||
{ | ||
/** | ||
* @return LogMessageProcessor | ||
*/ | ||
public static function build() | ||
{ | ||
$version = defined('\Monolog\Logger::API') ? MonologLogger::API : 1; | ||
|
||
switch ($version) { | ||
case 1: | ||
case 2: | ||
return new MonologMessageProcessor(); | ||
case 3: | ||
return new MonologV3MessageProcessor(); | ||
default: | ||
throw new RuntimeException('Version not supported'); | ||
} | ||
} | ||
} |
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