Logger.php 1.6 KB

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