| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Copyright 2024 Google Inc. All Rights Reserved. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
namespace Dudlewebs\WPMCS\GCP\Google\Auth\Logging; |
| 19 |
|
| 20 |
use InvalidArgumentException; |
| 21 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; |
| 22 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerTrait; |
| 23 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LogLevel; |
| 24 |
use Stringable; |
| 25 |
/** |
| 26 |
* A basic logger class to log into stdOut for GCP logging. |
| 27 |
* |
| 28 |
* @internal |
| 29 |
*/ |
| 30 |
class StdOutLogger implements LoggerInterface |
| 31 |
{ |
| 32 |
use LoggerTrait; |
| 33 |
/** |
| 34 |
* @var array<string,int> |
| 35 |
*/ |
| 36 |
private array $levelMapping = [LogLevel::EMERGENCY => 7, LogLevel::ALERT => 6, LogLevel::CRITICAL => 5, LogLevel::ERROR => 4, LogLevel::WARNING => 3, LogLevel::NOTICE => 2, LogLevel::INFO => 1, LogLevel::DEBUG => 0]; |
| 37 |
private int $level; |
| 38 |
/** |
| 39 |
* Constructs a basic PSR-3 logger class that logs into StdOut for GCP Logging |
| 40 |
* |
| 41 |
* @param string $level The level of the logger instance. |
| 42 |
*/ |
| 43 |
public function __construct(string $level = LogLevel::DEBUG) |
| 44 |
{ |
| 45 |
$this->level = $this->getLevelFromName($level); |
| 46 |
} |
| 47 |
/** |
| 48 |
* {@inheritdoc} |
| 49 |
*/ |
| 50 |
public function log($level, string|Stringable $message, array $context = []) : void |
| 51 |
{ |
| 52 |
if ($this->getLevelFromName($level) < $this->level) { |
| 53 |
return; |
| 54 |
} |
| 55 |
print $message . "\n"; |
| 56 |
} |
| 57 |
/** |
| 58 |
* @param string $levelName |
| 59 |
* @return int |
| 60 |
* @throws InvalidArgumentException |
| 61 |
*/ |
| 62 |
private function getLevelFromName(string $levelName) : int |
| 63 |
{ |
| 64 |
if (!\array_key_exists($levelName, $this->levelMapping)) { |
| 65 |
throw new InvalidArgumentException('The level supplied to the Logger is not valid'); |
| 66 |
} |
| 67 |
return $this->levelMapping[$levelName]; |
| 68 |
} |
| 69 |
} |
| 70 |
|