Skip to content

Commit

Permalink
Add conditional logging requests by LogRequest interceptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksucherek committed Jan 23, 2025
1 parent 2a03ede commit ecedf08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Ouzo/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Controller
private bool $keepMessage = false;
private SessionStats $sessionStats;
private ViewNameProvider $viewNameProvider;
private bool $loggerInterceptorEnabled = true;

public static function createInstance(RouteRule $routeRule, RequestParameters $requestParameters, SessionStats $sessionStats): Controller
{
Expand Down Expand Up @@ -196,6 +197,17 @@ public function __call(string $name, array $args)
throw new NoControllerActionException("No action [{$name}] defined in controller [{$class}].");
}


public function isLoggerInterceptorEnabled(): bool
{
return $this->loggerInterceptorEnabled;
}

public function setLoggerInterceptorEnabled(bool $loggerInterceptorEnabled): void
{
$this->loggerInterceptorEnabled = $loggerInterceptorEnabled;
}

private function wrapAsNotices(array $messages, ?string $url): array
{
return Arrays::map($messages, fn($msg) => new Notice($msg, $url));
Expand Down
14 changes: 8 additions & 6 deletions src/Ouzo/Core/Middleware/Interceptor/LogRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public function handle(mixed $param, Chain $next): mixed

private function handleRequestContext(RequestContext $requestContext, Chain $next): mixed
{
Logger::getLogger(__CLASS__)->info(sprintf('[Action: %s#%s] [Request: %s %s]',
$requestContext->getCurrentController(),
$requestContext->getCurrentAction(),
Uri::getRequestType(),
$this->pathProvider->getPath()
));
if ($requestContext->getCurrentControllerObject()?->isLoggerInterceptorEnabled()) {
Logger::getLogger(__CLASS__)->info(sprintf('[Action: %s#%s] [Request: %s %s]',
$requestContext->getCurrentController(),
$requestContext->getCurrentAction(),
Uri::getRequestType(),
$this->pathProvider->getPath()
));
}

return $next->proceed($requestContext);
}
Expand Down

0 comments on commit ecedf08

Please sign in to comment.