更新依赖库,新增数据库日志驱动;修改说明;

This commit is contained in:
augushong
2021-07-24 23:05:22 +08:00
parent 89d3840552
commit 1a132b1be9
9 changed files with 184 additions and 57 deletions

View File

@@ -15,4 +15,7 @@ CHARSET = utf8
DEBUG = true DEBUG = true
[LANG] [LANG]
default_lang = zh-cn default_lang = zh-cn
[LOG]
channel = debug_mysql

View File

@@ -96,6 +96,38 @@ php think reset_password
- 实现CMS后台 - 实现CMS后台
- 支持前台多主题 - 支持前台多主题
- 适配手机端,实现table转卡片样式 - 适配手机端,实现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`
### 完整安装 ### 完整安装

View File

@@ -6,6 +6,7 @@ use app\model\Category;
use app\model\Nav; use app\model\Nav;
use app\model\Post; use app\model\Post;
use app\model\PostCategory; use app\model\PostCategory;
use think\facade\Log;
use think\facade\View; use think\facade\View;
use think\Request; use think\Request;
@@ -20,6 +21,7 @@ class Index extends Common
{ {
// //
Log::record('测试日志');
return View::fetch(); return View::fetch();
} }

14
app/model/DebugLog.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class DebugLog extends Model
{
//
}

40
composer.lock generated
View File

@@ -449,16 +449,16 @@
}, },
{ {
"name": "topthink/framework", "name": "topthink/framework",
"version": "v6.0.8", "version": "v6.0.9",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/top-think/framework.git", "url": "https://github.com/top-think/framework.git",
"reference": "4789343672aef06d571d556da369c0e156609bce" "reference": "0b5fb453f0e533de3af3a1ab6a202510b61be617"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/framework/zipball/4789343672aef06d571d556da369c0e156609bce", "url": "https://api.github.com/repos/top-think/framework/zipball/0b5fb453f0e533de3af3a1ab6a202510b61be617",
"reference": "4789343672aef06d571d556da369c0e156609bce", "reference": "0b5fb453f0e533de3af3a1ab6a202510b61be617",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@@ -470,9 +470,9 @@
"require": { "require": {
"ext-json": "*", "ext-json": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"league/flysystem": "^1.0", "league/flysystem": "^1.1.4",
"league/flysystem-cached-adapter": "^1.0", "league/flysystem-cached-adapter": "^1.0",
"php": ">=7.1.0", "php": ">=7.2.5",
"psr/container": "~1.0", "psr/container": "~1.0",
"psr/log": "~1.0", "psr/log": "~1.0",
"psr/simple-cache": "^1.0", "psr/simple-cache": "^1.0",
@@ -514,9 +514,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/top-think/framework/issues", "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", "name": "topthink/think-captcha",
@@ -742,16 +742,16 @@
}, },
{ {
"name": "topthink/think-orm", "name": "topthink/think-orm",
"version": "v2.0.40", "version": "v2.0.44",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/top-think/think-orm.git", "url": "https://github.com/top-think/think-orm.git",
"reference": "1119d979b850849f3725856460cf108eec1c3eb8" "reference": "5d3d5c1ebf8bfccf34bacd90edb42989b16ea409"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/think-orm/zipball/1119d979b850849f3725856460cf108eec1c3eb8", "url": "https://api.github.com/repos/top-think/think-orm/zipball/5d3d5c1ebf8bfccf34bacd90edb42989b16ea409",
"reference": "1119d979b850849f3725856460cf108eec1c3eb8", "reference": "5d3d5c1ebf8bfccf34bacd90edb42989b16ea409",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@@ -797,9 +797,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/top-think/think-orm/issues", "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", "name": "topthink/think-template",
@@ -1156,16 +1156,16 @@
}, },
{ {
"name": "symfony/var-dumper", "name": "symfony/var-dumper",
"version": "v4.4.25", "version": "v4.4.26",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/var-dumper.git", "url": "https://github.com/symfony/var-dumper.git",
"reference": "31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0" "reference": "a586efdf2aa832d05b9249e9115d24f6a2691160"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0", "url": "https://api.github.com/repos/symfony/var-dumper/zipball/a586efdf2aa832d05b9249e9115d24f6a2691160",
"reference": "31ea689a8e7d2410016b0d25fc15a1ba05a6e2e0", "reference": "a586efdf2aa832d05b9249e9115d24f6a2691160",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@@ -1231,7 +1231,7 @@
"dump" "dump"
], ],
"support": { "support": {
"source": "https://github.com/symfony/var-dumper/tree/v4.4.25" "source": "https://github.com/symfony/var-dumper/tree/v4.4.26"
}, },
"funding": [ "funding": [
{ {
@@ -1247,7 +1247,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-05-27T09:48:32+00:00" "time": "2021-06-17T06:35:48+00:00"
} }
], ],
"aliases": [], "aliases": [],

View File

@@ -1,46 +1,50 @@
<?php <?php
use think\facade\Env; use think\facade\Env;
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | 日志设置 // | 日志设置
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
return [ return [
// 默认日志记录通道 // 默认日志记录通道
'default' => Env::get('log.channel', 'file'), 'default' => Env::get('log.channel', 'debug_mysql'),
// 日志记录级别 // 日志记录级别
'level' => [], 'level' => [],
// 日志类型记录的通道 ['error'=>'email',...] // 日志类型记录的通道 ['error'=>'email',...]
'type_channel' => [], 'type_channel' => [],
// 关闭全局日志写入 // 关闭全局日志写入
'close' => false, 'close' => false,
// 全局日志处理 支持闭包 // 全局日志处理 支持闭包
'processor' => null, 'processor' => null,
// 日志通道列表 // 日志通道列表
'channels' => [ 'channels' => [
'file' => [ 'file' => [
// 日志记录方式 // 日志记录方式
'type' => 'File', 'type' => 'File',
// 日志保存目录 // 日志保存目录
'path' => '', 'path' => '',
// 单文件日志写入 // 单文件日志写入
'single' => false, 'single' => false,
// 独立日志级别 // 独立日志级别
'apart_level' => [], 'apart_level' => [],
// 最大日志文件数量 // 最大日志文件数量
'max_files' => 0, 'max_files' => 0,
// 使用JSON格式记录 // 使用JSON格式记录
'json' => false, 'json' => false,
// 日志处理 // 日志处理
'processor' => null, 'processor' => null,
// 关闭通道日志写入 // 关闭通道日志写入
'close' => false, 'close' => false,
// 日志输出格式化 // 日志输出格式化
'format' => '[%s][%s] %s', 'format' => '[%s][%s] %s',
// 是否实时写入 // 是否实时写入
'realtime_write' => false, 'realtime_write' => false,
],
// 其它日志通道配置
], ],
'debug_mysql' => [
'type' => 'DebugMysql'
]
// 其它日志通道配置
],
]; ];

View File

@@ -0,0 +1,41 @@
<?php
use app\common\ColumnFormat;
use think\migration\Migrator;
use think\migration\db\Column;
class CreateTableDebugMysql extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}

1
extend/.gitignore vendored
View File

@@ -1,2 +1 @@
*
!.gitignore !.gitignore

View File

@@ -0,0 +1,32 @@
<?php
namespace think\log\driver;
use app\model\DebugLog;
use think\contract\LogHandlerInterface;
use think\facade\Log;
class DebugMysql implements LogHandlerInterface
{
public function save(array $log): bool
{
$create_time = time();
$create_time_title = date('Y-m-d H:i:s', $create_time);
foreach ($log as $log_level => $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;
}
}