ExceptionModule.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. namespace RTP\Module;
  19. class ExceptionModule extends \Exception
  20. {
  21. /**
  22. * 构造方法,传递错误信息以及错误码
  23. */
  24. public function __construct($code = 10000, $info)
  25. {
  26. //如果记录文件夹不存在则新建
  27. if (!file_exists('./log/'))
  28. {
  29. //如果新建失败则抛出异常,可能权限不足
  30. if (!mkdir('./log/'))
  31. {
  32. //此处不用ExceptionModule进行异常抛出,因为如果权限不足,此异常会无限抛出进入死循环
  33. throw new \Exception("can not create directory,please check you app's root file system authorization", 13001);
  34. }
  35. }
  36. parent::__construct($info, $code);
  37. }
  38. public function printError($isStop = FALSE)
  39. {
  40. //如果非调试模式,则取消所有的错误输出
  41. if (!DEBUG)
  42. {
  43. $infoJson = array('errorCode' => $this -> getCode());
  44. //输出json
  45. echo json_encode($infoJson);
  46. $infoJson = array(
  47. 'datetime' => date('Y/M/d H:i:s', time()),
  48. 'errorCode' => $this -> getCode(),
  49. 'info' => $this -> getMessage(),
  50. 'wrongFile' => $this -> getFile(),
  51. 'wrongLine' => $this -> getLine()
  52. );
  53. //将错误信息记录到文件
  54. $logInfo = "{$infoJson['datetime']}=>[code:{$infoJson['errorCode']};info:{$infoJson['info']};wrongFile:{$infoJson['wrongFile']};wrongLine:{$infoJson['wrongLine']}];\n";
  55. file_put_contents('./log/' . date('Y_M_d', time()) . '.txt', $logInfo, FILE_APPEND);
  56. }
  57. else
  58. {
  59. $infoJson = array(
  60. 'datetime' => date('Y/M/d H:i:s', time()),
  61. 'errorCode' => $this -> getCode(),
  62. 'info' => $this -> getMessage(),
  63. 'wrongFile' => $this -> getFile(),
  64. 'wrongLine' => $this -> getLine()
  65. );
  66. //输出自然语言
  67. printFormatted($infoJson);
  68. $logInfo = "{$infoJson['datetime']}=>[code:{$infoJson['errorCode']};info:{$infoJson['info']};wrongFile:{$infoJson['wrongFile']};wrongLine:{$infoJson['wrongLine']}];\n";
  69. file_put_contents('./log/' . date('Y_M_d', time()) . '.txt', $logInfo, FILE_APPEND);
  70. }
  71. if ($isStop)
  72. exit ;
  73. }
  74. }
  75. ?>