Logger.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay;
  4. use Yansongda\Pay\Contract\ConfigInterface;
  5. use Yansongda\Pay\Contract\LoggerInterface;
  6. use Yansongda\Pay\Exception\ContainerException;
  7. use Yansongda\Pay\Exception\InvalidConfigException;
  8. use Yansongda\Pay\Exception\ServiceNotFoundException;
  9. /**
  10. * @method static void emergency($message, array $context = [])
  11. * @method static void alert($message, array $context = [])
  12. * @method static void critical($message, array $context = [])
  13. * @method static void error($message, array $context = [])
  14. * @method static void warning($message, array $context = [])
  15. * @method static void notice($message, array $context = [])
  16. * @method static void info($message, array $context = [])
  17. * @method static void debug($message, array $context = [])
  18. * @method static void log($message, array $context = [])
  19. */
  20. class Logger
  21. {
  22. /**
  23. * @throws ContainerException
  24. * @throws ServiceNotFoundException
  25. * @throws InvalidConfigException
  26. */
  27. public static function __callStatic(string $method, array $args): void
  28. {
  29. if (!Pay::hasContainer() || !Pay::has(LoggerInterface::class)
  30. || false === Pay::get(ConfigInterface::class)->get('logger.enable', false)) {
  31. return;
  32. }
  33. $class = Pay::get(LoggerInterface::class);
  34. if ($class instanceof \Psr\Log\LoggerInterface || $class instanceof \Yansongda\Supports\Logger) {
  35. $class->{$method}(...$args);
  36. return;
  37. }
  38. throw new InvalidConfigException(Exception\Exception::LOGGER_CONFIG_ERROR);
  39. }
  40. }