Bläddra i källkod

feat(system): 添加扩展调试日志配置支持

- 新增 Log 类继承 think\Log 并注入扩展管理器
- 实现调试级别日志的动态配置加载
- 根据扩展配置决定是否启用 debug 日志级别
- 添加 LogTest 测试类验证日志功能
- 在测试中模拟扩展 ID 注入并验证 debug 方法调用
runphp 4 månader sedan
förälder
incheckning
dd2f87817d
2 ändrade filer med 59 tillägg och 0 borttagningar
  1. 35 0
      src/Log.php
  2. 24 0
      tests/LogTest.php

+ 35 - 0
src/Log.php

@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\System;
+
+use think\App;
+
+class Log extends \think\Log
+{
+    public function __construct(App $app, private ExtensionManager $extensionManager, private string $extensionID)
+    {
+        parent::__construct($app);
+    }
+
+    public function getConfig(?string $name = null, $default = null)
+    {
+        if ($name == 'level') {
+            return $this->getLevelConfig();
+        }
+        return parent::getConfig($name, $default);
+    }
+
+    private function getLevelConfig(): array
+    {
+        $level = parent::getConfig('level', []);
+        if (in_array('debug', $level)) {
+            return $level;
+        }
+        $debug = $this->extensionManager->getExtensionConfig($this->extensionID, 'debug');
+        if ($debug) {
+            $level[] = 'debug';
+        }
+        return $level;
+    }
+}

+ 24 - 0
tests/LogTest.php

@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\System;
+
+use PHPUnit\Framework\Attributes\Test;
+use PHPUnit\Framework\TestCase;
+
+class LogTest extends TestCase
+{
+    private Log $log;
+
+    protected function setUp(): void
+    {
+        $this->log = app()->make(Log::class, ['extensionID' => 'system'], true);
+    }
+
+    #[Test]
+    public function debug()
+    {
+        $this->log->debug('test1');
+        \think\facade\Log::debug('test2');
+    }
+}