diff --git a/.example.env b/.example.env index 7452f2e..ef8996b 100644 --- a/.example.env +++ b/.example.env @@ -15,4 +15,7 @@ CHARSET = utf8 DEBUG = true [LANG] -default_lang = zh-cn \ No newline at end of file +default_lang = zh-cn + +[LOG] +channel = debug_mysql \ No newline at end of file diff --git a/README.md b/README.md index a6e7b4b..9761c8b 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,38 @@ php think reset_password - 实现CMS后台 - 支持前台多主题 - 适配手机端,实现table转卡片样式 +- 数据库日志驱动 + +#### 默认开启数据库日志驱动 + +为了方便开发查看日志,系统中增加了数据库日志功能,此功能默认开启. + +显然使用mysql记录日志会有性能问题,项目上线后建议关闭,使用官方`File`或者其他. + +关闭数据库日志驱动:复制`.example.env`为`.env`, +将: +``` +[LOG] +channel = debug_mysql +``` +修改为: +``` +[LOG] +channel = file +``` + +或者修改`config/log.php`, +``` +// 默认日志记录通道 + 'default' => Env::get('log.channel', 'debug_mysql'), +``` +修改为: +``` +// 默认日志记录通道 + 'default' => Env::get('log.channel', 'file'), +``` + +> 虽然配置名称为`debug_mysql`,但实际上使用的是官方`think-orm`,仍然支持其它数据库,比如`sqlite` ### 完整安装 diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 3d0f232..28273df 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -6,6 +6,7 @@ use app\model\Category; use app\model\Nav; use app\model\Post; use app\model\PostCategory; +use think\facade\Log; use think\facade\View; use think\Request; @@ -20,6 +21,7 @@ class Index extends Common { // + Log::record('测试日志'); return View::fetch(); } diff --git a/app/model/DebugLog.php b/app/model/DebugLog.php new file mode 100644 index 0000000..43d181f --- /dev/null +++ b/app/model/DebugLog.php @@ -0,0 +1,14 @@ +=7.1.0", + "php": ">=7.2.5", "psr/container": "~1.0", "psr/log": "~1.0", "psr/simple-cache": "^1.0", @@ -514,9 +514,9 @@ ], "support": { "issues": "https://github.com/top-think/framework/issues", - "source": "https://github.com/top-think/framework/tree/v6.0.8" + "source": "https://github.com/top-think/framework/tree/v6.0.9" }, - "time": "2021-04-27T00:41:08+00:00" + "time": "2021-07-22T03:24:49+00:00" }, { "name": "topthink/think-captcha", @@ -742,16 +742,16 @@ }, { "name": "topthink/think-orm", - "version": "v2.0.40", + "version": "v2.0.44", "source": { "type": "git", "url": "https://github.com/top-think/think-orm.git", - "reference": "1119d979b850849f3725856460cf108eec1c3eb8" + "reference": "5d3d5c1ebf8bfccf34bacd90edb42989b16ea409" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/think-orm/zipball/1119d979b850849f3725856460cf108eec1c3eb8", - "reference": "1119d979b850849f3725856460cf108eec1c3eb8", + "url": "https://api.github.com/repos/top-think/think-orm/zipball/5d3d5c1ebf8bfccf34bacd90edb42989b16ea409", + "reference": "5d3d5c1ebf8bfccf34bacd90edb42989b16ea409", "shasum": "", "mirrors": [ { @@ -797,9 +797,9 @@ ], "support": { "issues": "https://github.com/top-think/think-orm/issues", - "source": "https://github.com/top-think/think-orm/tree/v2.0.40" + "source": "https://github.com/top-think/think-orm/tree/v2.0.44" }, - "time": "2021-04-19T13:29:37+00:00" + "time": "2021-07-21T02:22:31+00:00" }, { "name": "topthink/think-template", @@ -1156,16 +1156,16 @@ }, { "name": "symfony/var-dumper", - "version": "v4.4.25", + "version": "v4.4.26", "source": { "type": "git", "url": "https://github.com/symfony/var-dumper.git", - "reference": "31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0" + "reference": "a586efdf2aa832d05b9249e9115d24f6a2691160" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/var-dumper/zipball/31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0", - "reference": "31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/a586efdf2aa832d05b9249e9115d24f6a2691160", + "reference": "a586efdf2aa832d05b9249e9115d24f6a2691160", "shasum": "", "mirrors": [ { @@ -1231,7 +1231,7 @@ "dump" ], "support": { - "source": "https://github.com/symfony/var-dumper/tree/v4.4.25" + "source": "https://github.com/symfony/var-dumper/tree/v4.4.26" }, "funding": [ { @@ -1247,7 +1247,7 @@ "type": "tidelift" } ], - "time": "2021-05-27T09:48:32+00:00" + "time": "2021-06-17T06:35:48+00:00" } ], "aliases": [], diff --git a/config/log.php b/config/log.php index 5f7afcb..0d8cdde 100644 --- a/config/log.php +++ b/config/log.php @@ -1,46 +1,50 @@ Env::get('log.channel', 'file'), - // 日志记录级别 - 'level' => [], - // 日志类型记录的通道 ['error'=>'email',...] - 'type_channel' => [], - // 关闭全局日志写入 - 'close' => false, - // 全局日志处理 支持闭包 - 'processor' => null, + // 默认日志记录通道 + 'default' => Env::get('log.channel', 'debug_mysql'), + // 日志记录级别 + 'level' => [], + // 日志类型记录的通道 ['error'=>'email',...] + 'type_channel' => [], + // 关闭全局日志写入 + 'close' => false, + // 全局日志处理 支持闭包 + 'processor' => null, - // 日志通道列表 - 'channels' => [ - 'file' => [ - // 日志记录方式 - 'type' => 'File', - // 日志保存目录 - 'path' => '', - // 单文件日志写入 - 'single' => false, - // 独立日志级别 - 'apart_level' => [], - // 最大日志文件数量 - 'max_files' => 0, - // 使用JSON格式记录 - 'json' => false, - // 日志处理 - 'processor' => null, - // 关闭通道日志写入 - 'close' => false, - // 日志输出格式化 - 'format' => '[%s][%s] %s', - // 是否实时写入 - 'realtime_write' => false, - ], - // 其它日志通道配置 + // 日志通道列表 + 'channels' => [ + 'file' => [ + // 日志记录方式 + 'type' => 'File', + // 日志保存目录 + 'path' => '', + // 单文件日志写入 + 'single' => false, + // 独立日志级别 + 'apart_level' => [], + // 最大日志文件数量 + 'max_files' => 0, + // 使用JSON格式记录 + 'json' => false, + // 日志处理 + 'processor' => null, + // 关闭通道日志写入 + 'close' => false, + // 日志输出格式化 + 'format' => '[%s][%s] %s', + // 是否实时写入 + 'realtime_write' => false, ], + 'debug_mysql' => [ + 'type' => 'DebugMysql' + ] + // 其它日志通道配置 + ], ]; diff --git a/database/migrations/20210724150134_create_table_debug_mysql.php b/database/migrations/20210724150134_create_table_debug_mysql.php new file mode 100644 index 0000000..b6ca7f6 --- /dev/null +++ b/database/migrations/20210724150134_create_table_debug_mysql.php @@ -0,0 +1,41 @@ +table('debug_log') + ->setComment('日志表') + ->addColumn(ColumnFormat::stringNormal('title')) + ->addColumn(ColumnFormat::timestamp('create_time')) + ->addColumn(ColumnFormat::stringShort('create_time_title')) + ->addColumn(ColumnFormat::stringShort('level')) + ->addColumn(ColumnFormat::stringLong('content')) + ->create(); + } +} diff --git a/extend/.gitignore b/extend/.gitignore index c96a04f..b722e9e 100644 --- a/extend/.gitignore +++ b/extend/.gitignore @@ -1,2 +1 @@ -* !.gitignore \ No newline at end of file diff --git a/extend/think/log/driver/DebugMysql.php b/extend/think/log/driver/DebugMysql.php new file mode 100644 index 0000000..935b396 --- /dev/null +++ b/extend/think/log/driver/DebugMysql.php @@ -0,0 +1,32 @@ + $log_list) { + foreach ($log_list as $key => $log_item) { + DebugLog::create([ + 'level' => $log_level, + 'content' => $log_item, + 'create_time' => $create_time, + 'create_time_title' => $create_time_title, + ]); + } + } + + return true; + } +}