完成内置的数据库日志驱动;内置数据库日志的定时清除;更新新的数据库sql;

This commit is contained in:
2022-04-03 15:29:06 +08:00
parent dc1b3d4ab4
commit 2a97375418
12 changed files with 487 additions and 390 deletions

View File

@@ -1,10 +1,12 @@
<?php
declare (strict_types = 1);
declare(strict_types=1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\facade\Log;
use think\Validate;
/**
@@ -52,7 +54,10 @@ abstract class BaseController
// 初始化
protected function initialize()
{}
{
Log::debug('request url :' . $this->request->url());
}
/**
* 验证数据
@@ -90,5 +95,4 @@ abstract class BaseController
return $v->failException(true)->check($data);
}
}

View File

@@ -12,7 +12,6 @@
namespace app\admin\middleware;
use app\admin\service\SystemLogService;
use app\Request;
use EasyAdmin\tool\CommonTool;
use think\facade\Log;
@@ -48,14 +47,6 @@ class SystemLog
$method = strtolower($request->method());
$url = $request->url();
trace([
'url' => $url,
'method' => $method,
'params' => $params,
],
'requestDebugInfo'
);
if ($request->isAjax()) {
if (in_array($method, ['post', 'put', 'delete'])) {
$ip = CommonTool::getRealIp();
@@ -68,10 +59,9 @@ class SystemLog
'useragent' => $_SERVER['HTTP_USER_AGENT'],
'create_time' => time(),
];
SystemLogService::instance()->save($data);
Log::debug($data);
}
}
return $next($request);
}
}
}

View File

@@ -1,136 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\admin\service;
use think\facade\Db;
use think\facade\Config;
/**
* 系统日志表
* Class SystemLogService
* @package app\admin\service
*/
class SystemLogService
{
/**
* 当前实例
* @var object
*/
protected static $instance;
/**
* 表前缀
* @var string
*/
protected $tablePrefix;
/**
* 表后缀
* @var string
*/
protected $tableSuffix;
/**
* 表名
* @var string
*/
protected $tableName;
/**
* 构造方法
* SystemLogService constructor.
*/
protected function __construct()
{
$this->tablePrefix = Config::get('database.connections.mysql.prefix');
$this->tableSuffix = date('Ym', time());
$this->tableName = "{$this->tablePrefix}system_log_{$this->tableSuffix}";
return $this;
}
/**
* 获取实例对象
* @return SystemLogService|object
*/
public static function instance()
{
if (is_null(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
/**
* 保存数据
* @param $data
* @return bool|string
*/
public function save($data)
{
Db::startTrans();
try {
$this->detectTable();
Db::table($this->tableName)->insert($data);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return $e->getMessage();
}
return true;
}
/**
* 检测数据表
* @return bool
*/
protected function detectTable()
{
$check = Db::query("show tables like '{$this->tableName}'");
if (empty($check)) {
$sql = $this->getCreateSql();
Db::execute($sql);
}
return true;
}
public function getAllTableList()
{
}
/**
* 根据后缀获取创建表的sql
* @return string
*/
protected function getCreateSql()
{
return <<<EOT
CREATE TABLE `{$this->tableName}` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`admin_id` int(10) unsigned DEFAULT '0' COMMENT '管理员ID',
`url` varchar(1500) NOT NULL DEFAULT '' COMMENT '操作页面',
`method` varchar(50) NOT NULL COMMENT '请求方法',
`title` varchar(100) DEFAULT '' COMMENT '日志标题',
`content` text NOT NULL COMMENT '内容',
`ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'IP',
`useragent` varchar(255) DEFAULT '' COMMENT 'User-Agent',
`create_time` int(10) DEFAULT NULL COMMENT '操作时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=630 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='后台操作日志表 - {$this->tableSuffix}';
EOT;
}
}

View File

@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace app\common\command;
use app\common\tools\PathTools;
use PDO;
use think\Config as ThinkConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
@@ -34,6 +36,8 @@ class Install extends Command
protected function execute(Input $input, Output $output)
{
// 指令输出
$force = $input->getOption('force');
@@ -76,6 +80,12 @@ class Install extends Command
Db::startTrans();
try {
foreach ($sqlArray as $vo) {
if (strpos($vo, 'LOCK TABLES') === 0) {
continue;
}
if (strpos($vo, 'UNLOCK') === 0) {
continue;
}
Db::execute($vo);
}
Db::name('system_admin')

View File

@@ -2,9 +2,15 @@
return [
[
'name'=>'http_demo', // 定时任务的名称,不能重
'type'=>'site', // 定时任务的类型默认只支持site你也可以重写定时器命令行以支持其他命令
'target'=>'/tools/timer.ResetPassword/do', // 要访问的地址如果不是以https开头那么以后台的系统配置中读取相关配置如果没有配置则不执行
'frequency'=>600 // 执行频率单位填写10则每10秒过后执行一次
]
];
'name' => 'http_demo', // 定时任务的名称,不能重
'type' => 'site', // 定时任务的类型默认只支持site你也可以重写定时器命令行以支持其他命令
'target' => '/tools/timer.ResetPassword/do', // 要访问的地址如果不是以https开头那么以后台的系统配置中读取相关配置如果没有配置则不执行
'frequency' => 600 // 执行频率单位填写10则每10秒过后执行一次
],
[
'name' => 'clear_log', // 定时任务的名称,不能重复
'type' => 'site', // 定时任务的类型默认只支持site你也可以重写定时器命令行以支持其他命令
'target' => '/tools/timer.ClearLog/do', // 要访问的地址如果不是以https开头那么以后台的系统配置中读取相关配置如果没有配置则不执行
'frequency' => 600 // 执行频率单位填写10则每10秒过后执行一次
],
];

View File

@@ -11,6 +11,9 @@ class TimerController extends ToolsController
public function initialize()
{
parent::initialize();
if (is_integer($this->frequency)) {
$this->protectVisit($this->frequency);
}
@@ -21,7 +24,7 @@ class TimerController extends ToolsController
$cache_tag = 'timer_protect';
$cache_key = 'timer_protect_' . $this->request->url();
$cache_key = 'timer_protect_' . md5($this->request->url());
$last_exec_time = Cache::get($cache_key, 0);

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace app\tools\controller\timer;
use app\common\controller\TimerController;
use think\facade\Db;
use think\facade\Log;
use think\Request;
class ClearLog extends TimerController
{
protected $frequency = 600;
public function do()
{
Log::debug('清除3天的日志');
Db::name('debug_log')->where('create_time', '<', time() - 60 * 60 * 24 * 3)->delete();
return 'success';
}
}