Kaynağa Gözat

新增应用日志记录

Yahoo 4 yıl önce
ebeveyn
işleme
c2a88fd087

+ 56 - 0
app/common/library/Log.php

@@ -0,0 +1,56 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\library;
+
+use think\facade\Log as LogFacade;
+
+class Log
+{
+    // 日志内容
+    private static $data = [];
+
+    /**
+     * 写入日志
+     * @param mixed $value
+     * @param string $type
+     */
+    public static function record($value, string $type = 'info')
+    {
+        $content = is_string($value) ? $value : print_r($value, true);
+        LogFacade::record($content, $type);
+    }
+
+    /**
+     * 写入日志 (使用追加的方式, 索引值是name)
+     * @param string $name 日志记录名
+     * @param array $data 记录内容
+     */
+    public static function append(string $name, array $data)
+    {
+        $merge = array_merge(compact('name'), $data);
+        if (isset(static::$data[$name])) {
+            $merge = array_merge(static::$data[$name], $merge);
+        }
+        static::$data[$name] = $merge;
+    }
+
+    /**
+     * 在应用结束时将追加的日志数据写入到文件
+     */
+    public static function end()
+    {
+        foreach (static::$data as $name => $item) {
+            static::record(array_merge(['name' => $name], $item));
+        }
+    }
+}

+ 2 - 0
app/middleware.php

@@ -12,4 +12,6 @@ return [
     // 允许跨域请求 (如果没有跨域需求可将此处代码屏蔽以提升安全性)
     \cores\middleware\AllowCrossDomain::class,
 
+    // 应用日志记录
+    \cores\middleware\AppLog::class,
 ];

+ 1 - 17
cores/AppService.php

@@ -30,23 +30,7 @@ class AppService extends Service
     // 服务启动
     public function boot()
     {
-        // 记录访问日志
-        if (env('access_log') && !is_debug()) {
-            $log = $this->getVisitor();
-            $log .= "\r\n" . '[ header ] ' . print_r(Request::header(), true);
-            $log .= "" . '[ param ] ' . print_r(Request::param(), true);
-            $log .= "\r\n" . '--------------------------------------------------------------------------------------------';
-            Log::record($log, 'begin');
-        }
-    }
 
-    /**
-     * 获取请求路径信息
-     * @return string
-     */
-    private function getVisitor(): string
-    {
-        $data = [Request::ip(), Request::method(), Request::url(true)];
-        return implode(' ', $data);
     }
+
 }

+ 66 - 0
cores/middleware/AppLog.php

@@ -0,0 +1,66 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace cores\middleware;
+
+use think\Response;
+use think\facade\Log as FacadeLog;
+use app\common\library\Log;
+
+/**
+ * 中间件:应用日志
+ */
+class AppLog
+{
+    // 访问日志
+    private static $beginLog = '';
+
+    /**
+     * 前置中间件
+     * @param \think\Request $request
+     * @param \Closure $next
+     * @return mixed
+     */
+    public function handle(\think\Request $request, \Closure $next)
+    {
+        // 记录访问日志
+        if (env('begin_log')) {
+            $log = $this->getVisitor($request);
+            $log .= "\r\n" . '[ header ] ' . print_r($request->header(), true);
+            $log .= "" . '[ param ] ' . print_r($request->param(), true);
+            $log .= '--------------------------------------------------------------------------------------------';
+            static::$beginLog = $log;
+        }
+        return $next($request);
+    }
+
+    /**
+     * 记录访问日志
+     * @param Response $response
+     */
+    public function end(Response $response)
+    {
+        FacadeLog::record(static::$beginLog, 'begin');
+        Log::end();
+    }
+
+    /**
+     * 获取请求路径信息
+     * @param \think\Request $request
+     * @return string
+     */
+    private function getVisitor(\think\Request $request): string
+    {
+        $data = [$request->ip(), $request->method(), $request->url(true)];
+        return implode(' ', $data);
+    }
+}