stechstudio / backoff

PHP library providing retry functionality with multiple backoff strategies and jitter support
MIT License
181 stars 21 forks source link

FR: Logging #13

Closed arnegroskurth closed 11 months ago

arnegroskurth commented 2 years ago

Would be great to have some logging via an optionally passed in instance of Psr\Log\LoggerInterface. Would be interesting to log failed attempts with some information about the thrown exception and about the next retry strategy/delay.

jszobody commented 2 years ago

I agree! However, it seems there are a lot of different ways folks might want those logs to look. (Full stack trace or just the exception message? On every failure, or only after max attempts? In all environments, or just development? What log level?). It could quickly become a pile of options.

That's why I added the error handler callback so that users could handle logging on their own. Does that work for you?

arnegroskurth commented 2 years ago

Yes, that obviously covers the general case. But I think there could also be some default logging solution that can be used at least for rapid prototyping.

E.g.: Log a message Failed attempt %attempt%/%maxAttempts% (retry in %delay% ms): %exceptionMessage% without the stack-trace for failing attempts on the warning level and on the error level for the final retry. That will probably be adequate for a lot of use-cases.

Edit: Probably error and critical make more sense as logging levels.

jszobody commented 2 years ago

Ok, I could see that. Would probably need some sort of description as well, for the log message to be useful.

Failed to reach Stripe API (attempt %attempt%/%maxAttempts%)...

Which would suggest a method that might look like:

$backoff->logFailures('Failed to reach Stripe API', new Logger);

That seem reasonable?

As for logging levels, I think your first suggestion makes the most sense to me personally. A failure with retries left is just a warning since it could still succeed.

jszobody commented 11 months ago

Closing as stale. If there is still interest in the above approach and someone wants to take a crack at it, I'd be open to a PR. I have no need for it myself and am not planning to add this currently.