Commit b9a6f17b by 杨昕

添加日志

parent e62661fa
Pipeline #9965 passed with stages
in 36 seconds
......@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\Client;
use App\Exceptions\ControllerException;
use App\Exceptions\DatabaseException;
use App\Model\LogModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
......@@ -28,6 +29,8 @@ class UsersController extends Controller
$response = handler_drive(function () use ($request) {
LogModel::addlog(["用户登录"=>$request->all()]);
if (!$request->password) {
throw new ControllerException('口令不能为空',400);
......
<?php
namespace App\Model;
use App\Tool\Log;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class LogModel extends Eloquent
{
/**
* 与模型关联的数据表
* @var string
*/
protected $table = 'log';
# 是否自动维护时间戳
public $timestamps = false;
/**
* 添加一个日志
* @param $content
* @param int $type 1 一般 2 警告 3 错误
* @param string $uid 账户标示
*/
public static function addlog($content, int $type = 1, string $uid = '', $code = '')
{
$uri = $_SERVER['REQUEST_URI'] ?? 'cli';
//写入数据库
$data = ['msg' => $content, 'type' => $type, 'uri' => $uri];
// self::add($data);
//写入文件
$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
//根据类型不同,生成不同日志
if ($type == 1) {
app()->make('logs')::info($uid, $code, $jsonData);
} elseif ($type == 2) {
app()->make('logs')::warn($uid, $code, $jsonData);
} elseif ($type == 3) {
app()->make('logs')::fatal($uid, $code, $jsonData);
}
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@ class AppServiceProvider extends ServiceProvider
public function register()
{
//
$this->app->bind('logs',\App\Tool\Log::class);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment