[SOLVED] Logging middleware misses errors

I’m working on a web service build on Slim 4, and as part of this I have tried using the middlewares/access-log and custom middleware components with the aim of recording all request-response sessions, separately from the application logs.

These work well for successful requests, however they don’t log requests that result in an error response (for example, a 404)—obviously these requests fall through to the error middleware, for which I’m using the built-in class.

My question is, is there a neat way of ensuring the middleware records the request-response session before it drops through to the error handler?

Slim 4 uses a Last In, First Out (LIFO) model for middleware.

If you add the AccessLog middleware after the ErrorMiddleware, it will run first and can catch it.

1 Like

That seems to have done the trick! Thanks very much for your prompt advice.

I didn’t consider that approach previously, as the documentation comments indicate the error middleware should be registered after all others:

 * Note: This middleware should be added last. It will not handle any exceptions/errors
 * for middleware added after it.

Understanding the LIFO processing of the stack has really helped to clarify things, so thank you.