| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Console; |
| 4: | |
| 5: | use Psr\Log\InvalidArgumentException as PsrInvalidArgumentException; |
| 6: | use Psr\Log\LoggerInterface as PsrLoggerInterface; |
| 7: | use Psr\Log\LogLevel as PsrLogLevel; |
| 8: | use Salient\Contract\Console\ConsoleInterface as Console; |
| 9: | use Salient\Utility\Format; |
| 10: | use Throwable; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | class ConsoleLogger implements PsrLoggerInterface |
| 16: | { |
| 17: | private const LOG_LEVEL_MAP = [ |
| 18: | PsrLogLevel::EMERGENCY => Console::LEVEL_EMERGENCY, |
| 19: | PsrLogLevel::ALERT => Console::LEVEL_ALERT, |
| 20: | PsrLogLevel::CRITICAL => Console::LEVEL_CRITICAL, |
| 21: | PsrLogLevel::ERROR => Console::LEVEL_ERROR, |
| 22: | PsrLogLevel::WARNING => Console::LEVEL_WARNING, |
| 23: | PsrLogLevel::NOTICE => Console::LEVEL_NOTICE, |
| 24: | PsrLogLevel::INFO => Console::LEVEL_INFO, |
| 25: | PsrLogLevel::DEBUG => Console::LEVEL_DEBUG, |
| 26: | ]; |
| 27: | |
| 28: | private Console $Console; |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public function __construct(Console $console) |
| 34: | { |
| 35: | $this->Console = $console; |
| 36: | } |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function emergency($message, array $context = []) |
| 42: | { |
| 43: | $this->log(PsrLogLevel::EMERGENCY, $message, $context); |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public function alert($message, array $context = []) |
| 50: | { |
| 51: | $this->log(PsrLogLevel::ALERT, $message, $context); |
| 52: | } |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function critical($message, array $context = []) |
| 58: | { |
| 59: | $this->log(PsrLogLevel::CRITICAL, $message, $context); |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public function error($message, array $context = []) |
| 66: | { |
| 67: | $this->log(PsrLogLevel::ERROR, $message, $context); |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | public function warning($message, array $context = []) |
| 74: | { |
| 75: | $this->log(PsrLogLevel::WARNING, $message, $context); |
| 76: | } |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | public function notice($message, array $context = []) |
| 82: | { |
| 83: | $this->log(PsrLogLevel::NOTICE, $message, $context); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public function info($message, array $context = []) |
| 90: | { |
| 91: | $this->log(PsrLogLevel::INFO, $message, $context); |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function debug($message, array $context = []) |
| 98: | { |
| 99: | $msg1 = $this->applyContext((string) $message, $context, $ex); |
| 100: | $this->Console->debug($msg1, null, $ex, 1); |
| 101: | } |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | public function log($level, $message, array $context = []) |
| 107: | { |
| 108: | if (!isset(self::LOG_LEVEL_MAP[$level])) { |
| 109: | throw new PsrInvalidArgumentException('Invalid log level'); |
| 110: | } |
| 111: | |
| 112: | $msg1 = $this->applyContext((string) $message, $context, $ex); |
| 113: | $level = self::LOG_LEVEL_MAP[$level]; |
| 114: | $this->Console->message($msg1, null, $level, Console::TYPE_STANDARD, $ex); |
| 115: | } |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | private function applyContext(string $message, array $context, ?Throwable &$ex): string |
| 121: | { |
| 122: | if ($context) { |
| 123: | foreach ($context as $key => $value) { |
| 124: | $replace['{' . $key . '}'] = Format::value($value); |
| 125: | } |
| 126: | $message = strtr($message, $replace); |
| 127: | |
| 128: | if ( |
| 129: | isset($context['exception']) |
| 130: | && $context['exception'] instanceof Throwable |
| 131: | ) { |
| 132: | $ex = $context['exception']; |
| 133: | } |
| 134: | } |
| 135: | |
| 136: | return $message; |
| 137: | } |
| 138: | } |
| 139: | |