ApiLogs.php 3.3 KB

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