-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error logging by providing exception in context #3107
Comments
From PSR-3 specification:
It seems like it is perfectly legal to do that. Maybe we could add another parameter to |
@pdehne I think what you suggested makes no sense. You can already control the exception details logging through the $renderer = $this->callableResolver->resolve($this->logErrorRenderer);
$error = $renderer($this->exception, $this->logErrorDetails); By default If you want to set up the log display in some other way - you are welcome to Slim's docs - It explains how you can define you custom error handler. Also you can use @l0gicgate What do you think? |
Please let me explain my use case a bit more. If this is not something you like to support I can find a way around that, no problem. I would like to register the Monolog logger like so.
Then I would like to replace Slim's default logger with Monolog like so:
At the moment Sim's ErrorHandler does not provide the exception to Monolog as described above, so I can't configure Monlog to e.g. send 500 errors by mail but only log 404 errors to a file. As @t0mmy742 explained, providing the exception to a LoggerInterface implementation is explicitly mentioned and allowed in the PSR3-Specification. Would adding this have any drawbacks? I understand that there are other ways to make this work, e.g. by implementing my own Slim ErrorHandler, but if the exception would be provided it would work "out of the box" and Slim users would not need do this. But anyway, as mentioned above, if you don't want to support this, that's fine too. In that case feel free to close this issue. |
I would mention here that this specific approach (rely on an array key of the logger context) would imply some kind of indirect "contract", which I do not consider optimal. I think in your specific use-case you may consider implementing a custom ErrorMiddleware that catches only specific HTTP Exceptions and handles that individually, e.g. sending a mail, logging to a file etc. Then just "degrade" the Slim ErrorMiddleware as "fallback" for all other Exceptions (or remove it completely). |
Understood. Well, the PSR-3 specification would make this at least an official indirect contract :-) I thought the change would be a good addition to Slim in general, that's why I opened the issue to suggest it. I understand now that this is probably not the case. I can still use this approach, so no worries. I will subclass Slim's ErrorHandler, override the logError method like shown above and replace Slim's default ErrorHandler with my subclassed one. Feel free to close the issue. |
I think this is an interesting issue. Considering that the PSR-3 public function error($message, array $context = array()); There is room for context to be added which in this case would be the exception itself. An Interim Solution: You will need to extend <?php
namespace MyApp\Handlers;
use Slim\Handlers\ErrorHandler;
class MyCustomErrorHandler extends ErrorHandler {
protected function $logError(string $error): void
{
$this->logger->error($error, ['exception' => $this->exception]);
}
} index.php <?php
use MyApp\Handlers\MyCustomErrorHandler;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
// Build your container with your dependencies
$container = new Container();
// ...
// Add Error Middleware and Use Your Custom Error Handler
$errorMiddleware = $app->addErrorMiddleware(true, true, true, $container->get(LoggerInterface::class));
$errorMiddleware->setDefaultErrorHandler(MyCustomerErrorHandler::class);
// ...
$app->run(); We I guess we need to decided whether or not we pass the additional context to the |
When logging errors in Slim\Handlers\ErrorHandler logError the logger object is declared to implement the LoggerInterface and the error function of this interface is used to actually log the error:
The LoggerInterface error method has an optional second parameter named context that can be an array of anything. Supplying the exception in this array would improve error logging for some LoggerInterface implementations, e.g. with Monolog you could decide to log different kinds of exceptions differently, without the exception in the context this is not possible.
The only change needed would be:
Would it make sense to add this?
The text was updated successfully, but these errors were encountered: