ExceptionLogger.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. /**
  8. * Logs Exceptions
  9. *
  10. * We use this class to log exceptions while taking type-safety into account.
  11. */
  12. class ExceptionLogger
  13. {
  14. /** @var \Psr\Log\LoggerInterface */
  15. private $logger;
  16. /**
  17. * @param \Psr\Log\LoggerInterface $logger
  18. */
  19. public function __construct(\Psr\Log\LoggerInterface $logger)
  20. {
  21. $this->logger = $logger;
  22. }
  23. /**
  24. * Log a Warning
  25. *
  26. * @param \Exception $exception
  27. * @return void
  28. */
  29. public function warning(\Exception $exception)
  30. {
  31. do {
  32. $this->logger->warning($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
  33. } while ($exception = $exception->getPrevious());
  34. }
  35. /**
  36. * Log a Critical Issue
  37. *
  38. * @param \Exception $exception
  39. * @return void
  40. */
  41. public function critical(\Exception $exception)
  42. {
  43. do {
  44. $this->logger->critical($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
  45. } while ($exception = $exception->getPrevious());
  46. }
  47. /**
  48. * Log an Error
  49. *
  50. * @param \Exception $exception
  51. * @return void
  52. */
  53. public function error(\Exception $exception)
  54. {
  55. do {
  56. $this->logger->error($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
  57. } while ($exception = $exception->getPrevious());
  58. }
  59. }