ApiLogs.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * User: 136327134@qq.com
  5. * Date: 2019/4/12 11:19
  6. */
  7. namespace crmeb\services;
  8. /*
  9. * Api 日志和系统字段整合
  10. * class ApiLogs
  11. * */
  12. use think\Exception;
  13. use think\Log;
  14. class ApiLogs
  15. {
  16. // +----------------------------------------------------------------------
  17. // | 缓存前缀配置区域
  18. // +----------------------------------------------------------------------
  19. //ACCESS_TOKEN缓存前缀
  20. const ACCESS_TOKEN_PREFIX='AccessToken:';
  21. //api info 缓存前缀
  22. const AB_API_INFO='eb_ApiInfo:';
  23. //未支付订单取消
  24. const ORDER_UNPAID_PAGE='order_unpaid_page';
  25. // +----------------------------------------------------------------------
  26. // | 缓存时间配置区域
  27. // +----------------------------------------------------------------------
  28. //缓存时间
  29. const EXPIRE=86400;
  30. // +----------------------------------------------------------------------
  31. // | 系统预设字段明配置区域
  32. // +----------------------------------------------------------------------
  33. //access-token验证字段
  34. const ACCESS_TOKEN="access-token";
  35. //Api版本字段
  36. const API_VERSION='version';
  37. //用户token验证字段
  38. const USER_TOKEN='user-token';
  39. //系统预设日志
  40. protected static $logInfo=null;
  41. /*
  42. * 获取本类所有常量配置
  43. * @param string $code 常量名
  44. * @return array | string
  45. * */
  46. public static function getConstants($code='') {
  47. $oClass = new \ReflectionClass(__CLASS__);
  48. $stants=$oClass->getConstants();
  49. if($code) return isset($stants[$code]) ? $stants[$code] : '';
  50. else return $stants;
  51. }
  52. /*
  53. * 错误日志记录
  54. *
  55. * */
  56. public static function recodeErrorLog(Exception $exception)
  57. {
  58. $data=[
  59. 'code'=>$exception->getCode(),
  60. 'msg'=>$exception->getMessage(),
  61. 'file'=>$exception->getFile(),
  62. 'line'=>$exception->getLine(),
  63. ];
  64. $log="[{$data['code']}] {$data['msg']} [{$data['file']} : {$data['line']}]";
  65. self::writeLog($log,'e');
  66. }
  67. /*
  68. * 记录日志
  69. * $param string $contentlog 日志内容
  70. * $param string $typeLog 日志类型
  71. * $param string $dirLog 日志目录
  72. * */
  73. public static function writeLog($contentlog='',$typeLog='',$dirLog='ebapi')
  74. {
  75. Log::init([
  76. 'type' => 'File',
  77. 'path' => LOG_PATH.($dirLog ? $dirLog.'/' : '')
  78. ]);
  79. if($contentlog==='') $contentlog=self::$logInfo;
  80. if($contentlog===null) return false;
  81. if(is_array($contentlog)) $contentlog=var_export($contentlog,true);
  82. if(is_object($contentlog)) $contentlog=var_export($contentlog,true);
  83. switch (strtoupper($typeLog)){
  84. case 'SQL':case 'S':
  85. Log::sql($contentlog);
  86. break;
  87. case 'ERROR':case 'E':
  88. Log::error($contentlog);
  89. break;
  90. case 'INFO':case 'I':
  91. Log::info($contentlog);
  92. break;
  93. case 'NOTICE':case 'N':
  94. Log::notice($contentlog);
  95. break;
  96. case 'ALERT':case 'A':
  97. Log::alert($contentlog);
  98. break;
  99. case 'LOG':case 'L':
  100. Log::log($contentlog);
  101. break;
  102. }
  103. }
  104. }