Log.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/3/2
  6. * Time: 下午1:29
  7. */
  8. namespace common\helpers;
  9. use common\models\LogAdminHandle;
  10. use common\models\LogAdminLogin;
  11. use common\models\LogApiNotice;
  12. use common\models\LogAsync;
  13. use common\models\Period;
  14. use Yii;
  15. use yii\helpers\Json;
  16. class Log
  17. {
  18. /**
  19. * 管理员操作日志
  20. * @param $message
  21. * @param int $isKey
  22. * @param null $userId
  23. * @param null $userName
  24. * @param null $remark
  25. */
  26. public static function adminHandle($message, $isKey = 0, $userId = null, $userName = null, $remark = null){
  27. if(Yii::$app->params['enableLog']){
  28. $period = Period::instance();
  29. $deviceInfo = Yii::$app->request->getDeviceInfo();
  30. $userInfo = Yii::$app->user->getUserInfo();
  31. $model = new LogAdminHandle();
  32. $model->ADMIN_ID = $userInfo['id'];
  33. $model->ADMIN_NAME = $userInfo['adminName'];
  34. $model->IP = $userInfo['ip'];
  35. $model->REQUEST_ROUTE = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;
  36. $model->OPT_CONTENT = $message;
  37. $model->KEY_LOG = $isKey;
  38. $model->OPT_OBJ_ID = $userId;
  39. $model->OPT_OBJ_NAME = $userName;
  40. $model->REMARK = $remark;
  41. $model->DEVICE_TYPE = Yii::$app->request->getDevice();
  42. $model->DEVICE_SYSTEM = $deviceInfo ? $deviceInfo['system'] : null;
  43. $model->DEVICE_VERSION = $deviceInfo ? $deviceInfo['version'] : null;
  44. $model->DEVICE_NET = $deviceInfo ? $deviceInfo['networkType'] : null;
  45. $model->DEVICE_UUID = $deviceInfo ? $deviceInfo['uuid'] : null;
  46. $model->USER_AGENT = Yii::$app->request->getUserAgent();
  47. $model->P_MONTH = Date::ociToDate();
  48. $model->CREATED_AT = Date::nowTime();
  49. $model->PERIOD_NUM = $period->getNowPeriodNum();
  50. $model->save();
  51. }
  52. }
  53. /**
  54. * 管理员登录日志
  55. */
  56. public static function adminLogin(){
  57. if(Yii::$app->params['enableLog']) {
  58. $period = Period::instance();
  59. $userInfo = Yii::$app->user->getUserInfo();
  60. $deviceInfo = Yii::$app->request->getDeviceInfo();
  61. $model = new LogAdminLogin();
  62. $model->ADMIN_ID = $userInfo['id'];
  63. $model->ADMIN_NAME = $userInfo['adminName'];
  64. $model->IP = $userInfo['ip'];
  65. $model->P_MONTH = Date::ociToDate();
  66. $model->DEVICE_TYPE = Yii::$app->request->getDevice();
  67. $model->DEVICE_SYSTEM = $deviceInfo ? $deviceInfo['system'] : null;
  68. $model->DEVICE_VERSION = $deviceInfo ? $deviceInfo['version'] : null;
  69. $model->DEVICE_NET = $deviceInfo ? $deviceInfo['networkType'] : null;
  70. $model->DEVICE_UUID = $deviceInfo ? $deviceInfo['uuid'] : null;
  71. $model->USER_AGENT = Yii::$app->request->getUserAgent();
  72. $model->CREATED_AT = Date::nowTime();
  73. $model->PERIOD_NUM = $period->getNowPeriodNum();
  74. $model->save();
  75. }
  76. }
  77. /**
  78. * 接口异步通知日志
  79. * @param $param
  80. */
  81. public static function apiNotice($param){
  82. if(Yii::$app->params['enableLog']) {
  83. [
  84. 'url' => $url,
  85. 'method' => $method,
  86. 'data' => $data,
  87. 'route' => $route,
  88. 'status' => $status,
  89. 'response' => $response,
  90. 'remark' => $remark,
  91. ] = $param;
  92. $model = new LogApiNotice();
  93. $model->URL = $url;
  94. $model->METHOD = $method;
  95. $model->DATA = Json::encode($data);
  96. $model->ROUTE = $route;
  97. $model->STATUS = $status;
  98. $model->RESPONSE = Json::encode($response);
  99. $model->REMARK = $remark;
  100. $model->P_MONTH = Date::ociToDate();
  101. $model->CREATED_AT = Date::nowTime();
  102. $model->save();
  103. }
  104. }
  105. /**
  106. * console中异步任务的执行日志
  107. * @param $param
  108. */
  109. public static function async($param){
  110. // file_put_contents(Yii::getAlias('@common/runtime/logs/asyncLog.log'), var_export($param, true));
  111. if(Yii::$app->params['enableLog']) {
  112. [
  113. 'type' => $type,
  114. 'route' => $route,
  115. 'title' => $title,
  116. 'detail' => $detail,
  117. 'status' => $status,
  118. ] = $param;
  119. $model = new LogAsync();
  120. $model->TYPE = $type;
  121. $model->ROUTE = $route;
  122. $model->TITLE = $title;
  123. $model->DETAIL = $detail;
  124. $model->STATUS = $status;
  125. $model->P_MONTH = Date::ociToDate();
  126. $model->CREATED_AT = Date::nowTime();
  127. $model->save();
  128. }
  129. }
  130. /**
  131. * 文件日志
  132. * @param $message
  133. * @param $category
  134. */
  135. public static function cliInfo($message, $category = 'application'){
  136. Yii::getLogger()->flush(true);
  137. Yii::info($message, $category);
  138. }
  139. }