1010
1111namespace Slim ;
1212
13+ use Psr \Container \ContainerExceptionInterface ;
1314use Psr \Container \ContainerInterface ;
15+ use Psr \Container \NotFoundExceptionInterface ;
1416use Psr \Http \Message \ResponseInterface ;
1517use Psr \Http \Message \ServerRequestInterface ;
1618use Psr \Http \Server \MiddlewareInterface ;
1719use Psr \Http \Server \RequestHandlerInterface ;
20+ use Psr \Log \LoggerInterface ;
1821use Slim \Interfaces \EmitterInterface ;
1922use Slim \Interfaces \RouterInterface ;
2023use Slim \Interfaces \ServerRequestCreatorInterface ;
2124use Slim \Middleware \EndpointMiddleware ;
2225use Slim \Middleware \ErrorExceptionMiddleware ;
26+ use Slim \Middleware \ExceptionLoggingMiddleware ;
2327use Slim \Middleware \HtmlExceptionMiddleware ;
2428use Slim \Middleware \JsonExceptionMiddleware ;
2529use Slim \Middleware \RoutingMiddleware ;
@@ -126,6 +130,7 @@ public function group(string $path, callable $handler): RouteGroup
126130
127131 /**
128132 * Set the base path used for routing.
133+ *
129134 * @param string $basePath
130135 */
131136 public function setBasePath (string $ basePath ): self
@@ -145,7 +150,6 @@ public function getBasePath(): string
145150
146151 /**
147152 * Add a new middleware to the stack.
148- * @param MiddlewareInterface|callable|string $middleware
149153 */
150154 public function add (MiddlewareInterface |callable |string $ middleware ): self
151155 {
@@ -156,7 +160,6 @@ public function add(MiddlewareInterface|callable|string $middleware): self
156160
157161 /**
158162 * Add a new middleware to the application's middleware stack.
159- * @param MiddlewareInterface $middleware
160163 */
161164 public function addMiddleware (MiddlewareInterface $ middleware ): self
162165 {
@@ -178,16 +181,39 @@ public function addRoutingMiddleware(): self
178181 }
179182
180183 /**
181- * Add set of default error handling middleware.
184+ * Add a set of default error handling middleware.
185+ *
186+ * @param bool $displayErrorDetails
187+ * @param bool $logErrors
188+ * @param bool $logErrorDetails
189+ * @param LoggerInterface|null $logger
182190 *
183191 * @return self
184192 */
185- public function addErrorMiddleware (): self
186- {
187- return $ this
193+ public function addErrorMiddleware (
194+ bool $ displayErrorDetails = false ,
195+ bool $ logErrors = true ,
196+ bool $ logErrorDetails = true ,
197+ ?LoggerInterface $ logger = null ,
198+ ): self {
199+ $ app = $ this
188200 ->add (ErrorExceptionMiddleware::class)
189- ->add (HtmlExceptionMiddleware::class)
201+ ->add ($ this -> container -> get ( HtmlExceptionMiddleware::class)-> withErrorDetails ( $ displayErrorDetails ) )
190202 ->add (JsonExceptionMiddleware::class);
203+
204+ if ($ logErrors ) {
205+ $ loggingMiddleware = $ this ->container
206+ ->get (ExceptionLoggingMiddleware::class)
207+ ->withLogErrorDetails ($ logErrorDetails );
208+
209+ if ($ logger ) {
210+ $ loggingMiddleware = $ loggingMiddleware ->withLogger ($ logger );
211+ }
212+
213+ $ app ->add ($ loggingMiddleware );
214+ }
215+
216+ return $ app ;
191217 }
192218
193219 /**
@@ -211,7 +237,6 @@ public function run(?ServerRequestInterface $request = null): void
211237 *
212238 * This method processes the request through the application's middleware stack and router,
213239 * returning the resulting HTTP response.
214- * @param ServerRequestInterface $request
215240 */
216241 public function handle (ServerRequestInterface $ request ): ResponseInterface
217242 {
0 commit comments