WriteLog.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\common\listener;
  12. use app\admin\service\AdminAuthService;
  13. use app\common\model\AdminLog;
  14. use think\facade\Log;
  15. use think\exception\HttpException;
  16. class WriteLog
  17. {
  18. const BEGIN=1;
  19. const ADMIN=2;
  20. const END=3;
  21. public static $starttime;
  22. //不记录日志的控制器
  23. private $noWriteLog=[
  24. [
  25. 'controller'=>'app\\admin\\controller\\Ajax',
  26. 'action'=>'*'
  27. ],
  28. [
  29. 'controller'=>'app\\admin\\controller\\Index',
  30. 'action'=>['captcha','checklogin']
  31. ]
  32. ];
  33. public function handle($event)
  34. {
  35. $request=request();
  36. $modulename=app('http')->getName();
  37. $controllername = $request->controller();
  38. $actionname = $request->action();
  39. if(!$this->checkLog($modulename,$controllername,$actionname)){
  40. return;
  41. }
  42. if(is_numeric($event)){
  43. switch ($event){
  44. case self::BEGIN:
  45. self::$starttime=microtime(true);
  46. Log::record("Url:{$request->url()} Method:{$request->method()}");
  47. Log::record("----------{$modulename}----------{$controllername}----------{$actionname}----------");
  48. break;
  49. case self::END:
  50. $usetime=round(microtime(true)-self::$starttime,5);
  51. Log::record("请求完成,耗时:".$usetime."秒\n");
  52. Log::save();
  53. break;
  54. case self::ADMIN:
  55. $this->writeAdminLog();
  56. break;
  57. }
  58. }
  59. if(is_string($event)){
  60. Log::record($event);
  61. }
  62. if($event instanceof \Exception){
  63. log::save();
  64. }
  65. }
  66. public function writeAdminLog()
  67. {
  68. if(!request()->isPost()){
  69. return;
  70. }
  71. $auth = AdminAuthService::newInstance();
  72. if(!$auth->isLogin()){
  73. return;
  74. }
  75. $controllername=$auth->getRoute('controllername');
  76. $actionname=$auth->getRoute('actionname');
  77. $content = $this->parseContent(request()->post());
  78. if(empty($content)){
  79. $content='';
  80. }else{
  81. $content=json_encode($content,JSON_UNESCAPED_UNICODE);
  82. }
  83. $url=$auth->getPath($controllername,$actionname);
  84. $insert=[
  85. 'admin_id'=>$auth->id,
  86. 'username'=>$auth->username,
  87. 'controller'=>$controllername,
  88. 'action'=>$actionname,
  89. 'url'=>$url,
  90. 'title'=>$auth->getRoute('title'),
  91. 'content'=>$content,
  92. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  93. 'ip'=>request()->ip()
  94. ];
  95. (new AdminLog())->save($insert);
  96. }
  97. private function checkLog($modulename,$controllername,$actionname)
  98. {
  99. $controllername = 'app\\'.$modulename.'\\controller\\'.str_replace('.','\\',$controllername);
  100. foreach ($this->noWriteLog as $item){
  101. if($item['controller']==$controllername){
  102. if(is_string($item['action'])){
  103. $item['action']=explode(',',$item['action']);
  104. }
  105. if(in_array('*',$item['action']) || in_array($actionname,$item['action'])){
  106. return false;
  107. }
  108. }
  109. }
  110. return true;
  111. }
  112. //递归将数组arr中key为password和token的值置空
  113. private function parseContent(array $arr)
  114. {
  115. foreach ($arr as $key=>$val){
  116. if(is_array($val)){
  117. $arr[$key]=$this->parseContent($val);
  118. }else{
  119. if($key=='password' || $key=='token'){
  120. $arr[$key]='******';
  121. }
  122. }
  123. }
  124. return $arr;
  125. }
  126. }