'; private const string BARE_TOKEN_PLACEHOLDER = ''; #[Inject] protected Request $request; public function logException(Throwable $e): void { $previous = $e->getPrevious(); Functions::logException(new RuntimeException('secrets.exception: ' . self::redact($e->getMessage())), context: [ 'class' => $e::class, 'file' => $e->getFile() . ':' . $e->getLine(), 'trace' => self::redact($e->getTraceAsString()), 'previousClass' => $previous !== null ? $previous::class : null, 'previousMessage' => $previous !== null ? self::redact($previous->getMessage()) : null, 'path' => self::redact((string) $this->request->getBaseUrl()), 'host' => gethostname(), 'ipHash' => $this->clientIpHash(), 'public' => true, ]); } /** * @param array $context */ public function logWarning(string $message, array $context = []): void { Functions::logWarning($message, $this->sanitizeContext($context)); } public static function redact(string $text): string { if ($text === '') { return $text; } $text = (string) preg_replace(self::TOKEN_PATTERN, self::TOKEN_PLACEHOLDER, $text); return (string) preg_replace(self::BARE_TOKEN_PATTERN, self::BARE_TOKEN_PLACEHOLDER, $text); } /** * @param array $context * @return array */ private function sanitizeContext(array $context): array { $context['public'] = true; if (!isset($context['ipHash'])) { $context['ipHash'] = $this->clientIpHash(); } if (isset($context['path']) && StringHelper::isString($context['path'])) { $context['path'] = self::redact($context['path']); } return $context; } private function clientIpHash(): string { $ip = (string) $this->request->getClientIp(); if ($ip === '') { return ''; } return Functions::hashIp($ip); } }