Here’s the setup (and maybe someone can offer an alternative to my current system):
Application Middleware (added in this order, so executed in reverse):
- ErrorMiddleware (w/ custom ErrorHandler)
- RoutingMiddleware
- CustomMiddleware (determines api version and therefore which directory/files to
include
)
Reasoning:
I’m adding RoutingMW after the ErrorMW so when they run in reverse I have access to any routing info during my error middleware/handler. Also, I don’t know why I can’t put my CustomMW first and therefore run it last (it throws a status 500, and I can’t seem to debug it).
When I set my ErrorMW, I also create my custom ErrorHandler and pass it to the ErrorMW as the default handler. I want the ErrorHandler to have a logger, so I set the DI container’s logger to write into my “lastest api version” directory (say, /logs/v2.0/api-log
), and inject that into the ErrorHandler. When the app runs and finally determines the right API version, I overwrite the DI container’s logger with the chosen API version for the log file directory, and then inject that logger in my various controllers as needed. If the user is running an old app and needs an older API like 1.0
, the application will log non-errors to the correct /logs/v1.0/api-log
, but the ErrorHandler will still reference the old Logger I gave it during the application Middleware setup (/logs/v2.0/api-log
), so any errors are written there instead.
Is there a way, during the application, to change the custom ErrorHandler’s logger and set it to the correct 1.0
path?
Hope that made sense,
Thanks!