|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\ubc_cwl_auth\Logger; |
| 4 | + |
| 5 | +use Drupal\Core\Logger\LoggerChannelInterface; |
| 6 | +use Drupal\ubc_cwl_auth\Debug\SamlauthDebugReactor; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use Psr\Log\LogLevel; |
| 9 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 10 | +use Drupal\Core\Session\AccountInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Wraps the samlauth logger channel so we can react to debug() messages. |
| 14 | + */ |
| 15 | +class SamlauthChannelWrapper implements LoggerChannelInterface { |
| 16 | + |
| 17 | + protected LoggerChannelInterface $channel; |
| 18 | + protected SamlauthDebugReactor $reactor; |
| 19 | + |
| 20 | + public function __construct(LoggerChannelInterface $channel, SamlauthDebugReactor $reactor) { |
| 21 | + $this->channel = $channel; |
| 22 | + $this->reactor = $reactor; |
| 23 | + } |
| 24 | + |
| 25 | + // --- PSR-3 methods --- |
| 26 | + public function emergency(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::EMERGENCY, $message, $context); } |
| 27 | + public function alert(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::ALERT, $message, $context); } |
| 28 | + public function critical(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::CRITICAL, $message, $context); } |
| 29 | + public function error(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::ERROR, $message, $context); } |
| 30 | + public function warning(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::WARNING, $message, $context); } |
| 31 | + public function notice(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::NOTICE, $message, $context); } |
| 32 | + public function info(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::INFO, $message, $context); } |
| 33 | + public function debug(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::DEBUG, $message, $context); } |
| 34 | + public function log($level, \Stringable|string $message, array $context = []): void { |
| 35 | + if ($level === LogLevel::DEBUG) { |
| 36 | + try { |
| 37 | + $this->reactor->handleDebugMessage((string) $message, $context); |
| 38 | + } |
| 39 | + catch (\Throwable $e) { |
| 40 | + $this->channel->error('ubc_cwl_auth reactor error: @msg', ['@msg' => $e->getMessage()]); |
| 41 | + } |
| 42 | + } |
| 43 | + $this->channel->log($level, $message, $context); |
| 44 | + } |
| 45 | + |
| 46 | + // --- Drupal-specific methods --- |
| 47 | + public function setRequestStack(?RequestStack $requestStack = null): void { |
| 48 | + if (method_exists($this->channel, 'setRequestStack')) { |
| 49 | + $this->channel->setRequestStack($requestStack); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function setCurrentUser(?AccountInterface $account = null): void { |
| 54 | + if (method_exists($this->channel, 'setCurrentUser')) { |
| 55 | + $this->channel->setCurrentUser($account); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public function setLoggers(array $loggers = []): void { |
| 60 | + if (method_exists($this->channel, 'setLoggers')) { |
| 61 | + $this->channel->setLoggers($loggers); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public function addLogger(LoggerInterface $logger, $priority = 0): void { |
| 66 | + if (method_exists($this->channel, 'addLogger')) { |
| 67 | + $this->channel->addLogger($logger, $priority); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function getName(): string { |
| 72 | + if (method_exists($this->channel, 'getName')) { |
| 73 | + return $this->channel->getName(); |
| 74 | + } |
| 75 | + return ''; |
| 76 | + } |
| 77 | +} |
0 commit comments