ErrHelper.php 749 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace common\helpers;
  3. use Exception;
  4. use Yii;
  5. /**
  6. * 错误助手类
  7. * @package common\helpers
  8. */
  9. final class ErrHelper
  10. {
  11. /**
  12. * 获取异常信息
  13. *
  14. * @param Exception $e
  15. * @return string
  16. */
  17. public static function getExceptionMsg(Exception $e)
  18. {
  19. return sprintf('错误文件: %s, 错误行数: %s, 错误内容: %s', $e->getFile(), $e->getLine(), $e->getMessage());
  20. }
  21. /**
  22. * 记录错误信息到日志
  23. *
  24. * @param string $msg
  25. * @param string $params
  26. * @return void
  27. */
  28. public static function log(string $title, Exception $e)
  29. {
  30. Yii::$app->custom->logs(
  31. $title . '「' . static::getExceptionMsg($e) . '」'
  32. );
  33. }
  34. }