Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
jarviscdr committed Feb 25, 2024
0 parents commit ae65f1e
Show file tree
Hide file tree
Showing 61 changed files with 7,497 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
/.vscode
/vendor
*.log
.env
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 walkor<[email protected]> and contributors (see https://github.com/walkor/webman/contributors)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# LogC 日志中心
这是一个使用PostgreSQL存储数据的日志中心服务。
基于webman进行开发,可以使用HTTP或WebSocket两种方式将日志发送到日志中心。
并提供一个简易的UI进行日志搜索。

## 起源
由于多项目日志查看非常不方便的原因,打算使用一个日志中心的服务,但是因为ELK太重了,而且大部分时间只是使用查询的功能,所以就诞生了这个LogC日志中心;

## 安装
```bash
git clone https://github.com/jarviscdr/logc.git

composer install

php webman start
```

## 使用
提供了一个配套的工具库,可前往[logc-client](https://github.com/jarviscdr/logc-client)查看
37 changes: 37 additions & 0 deletions app/bootstrap/PostgreSQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace app\bootstrap;

use Exception;
use Illuminate\Database\Events\QueryExecuted;
use support\Db;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Webman\Bootstrap;

class PostgreSQL implements Bootstrap
{
public static function start($worker)
{
if($worker->id != 1) {
return;
}

$extname = 'pg_jieba';

$extensionResult = Db::table('pg_extension')->where('extname', $extname)->first();
if(!empty($extensionResult)) {
return;
}

try{
Db::statement('create extension pg_jieba');
}catch(Exception $th) {
VD($th->getMessage());
}

// 使用分词扩展
// select * from to_tsvector('jiebacfg', '分词文本');
}

}
64 changes: 64 additions & 0 deletions app/bootstrap/SqlDebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace app\bootstrap;

use Illuminate\Database\Events\QueryExecuted;
use support\Db;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Webman\Bootstrap;

class SqlDebug implements Bootstrap
{
/**
* 自定义输出格式,否则输出前面会带有当前文件,无用信息
* @param $var
* @return void
*/
public static function dumpvar($var): void
{
$cloner = new VarCloner();
$dumper = new CliDumper();
$dumper->dump($cloner->cloneVar($var));
}

public static function start($worker)
{
// Is it console environment ?
$is_console = !$worker;
if ($is_console) {
// If you do not want to execute this in console, just return.
return;
}
if (!config("app.debug") || config("app.debug") === 'false') return;
$appPath = app_path();

Db::connection()->listen(function (QueryExecuted $queryExecuted) use ($appPath) {
if (isset($queryExecuted->sql) and $queryExecuted->sql !== "select 1") {
$bindings = $queryExecuted->bindings;
$sql = array_reduce(
$bindings,
function ($sql, $binding) {
return preg_replace('/\?/', is_numeric($binding) ? $binding : "'" . $binding . "'", $sql, 1);
},
$queryExecuted->sql
);

// self::dumpvar("[sql] [time:{$queryExecuted->time} ms] [{$sql}]"); // 这句话是打印所有的sql
// 下面是只打印app目录下产生的sql语句
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($traces as $trace) {
if (isset($trace['file']) && isset($trace["function"])) {
if (str_contains($trace['file'], $appPath)) {
$file = str_replace(base_path(), '', $trace['file']);
$str = "[file] {$file}:{$trace['line']} [function]:{$trace["function"]}";
self::dumpvar("[sql] [time:{$queryExecuted->time} ms] [{$sql}]");
self::dumpvar($str);
}
}
}
}
});
}

}
22 changes: 22 additions & 0 deletions app/controller/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace app\controller;

use taoser\exception\ValidateException;

class BaseController
{
public function validate($validateClass, $scene = '')
{
try {
$validator = validate($validateClass);
if (!empty($scene)) {
$validator->scene($scene);
}
$validator->check(request()->all());
return $validator->getSafeValue();
} catch (ValidateException $e) {
BE($e->getError());
}
}
}
32 changes: 32 additions & 0 deletions app/controller/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace app\controller;

use app\service\LogService;
use app\service\ProjectService;
use support\Db;
use support\Request;

class IndexController
{
public function __construct(
protected ProjectService $projectService
) {
}

/**
* 日志视图
*
* @param Request $request
* @return void
* @author Jarvis
* @date 2024-02-18 22:37
*/
public function index(Request $request)
{
return view('log/index', [
'projects' => $this->projectService->list(),
'types' => LogService::LOG_TYPE_LIST
]);
}
}
128 changes: 128 additions & 0 deletions app/controller/api/LogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace app\controller\api;

use app\controller\BaseController;
use app\service\LogService;
use app\validate\LogSearchValidate;
use app\validate\LogValidate;
use support\Request;
use support\Response;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Timer;

/**
* 日志控制器
*
* @author Jarvis
* @date 2024-02-18 22:37
*/
class LogController extends BaseController
{
public function __construct(
protected LogService $logService
) {
}

/**
* 记录日志
*
* @return Response
* @author Jarvis
* @date 2024-02-16 23:03
*/
public function record()
{
$data = $this->validate(LogValidate::class, 'create');
$id = $this->logService->create($data);
return success(['id' => $id]);
}

/**
* 日志详情
*
* @return Response
* @author Jarvis
* @date 2024-02-17 22:19
*/
public function info()
{
$data = $this->validate(LogValidate::class, 'info');
$data = $this->logService->info($data['id']);
return success($data);
}

/**
* 搜索日志
*
* @return Response
* @author Jarvis
* @date 2024-02-17 22:18
*/
public function search()
{
$where = $this->validate(LogSearchValidate::class);
$data = $this->logService->search($where);

$words = [];
if(!empty($where['content'])) {
$words = $this->logService->wordSegmentation($where['content']);
}
return success(['words' => $words, 'list' => $data]);
}

/**
* 监听日志
*
* @param Request $request
* @return Response
* @author Jarvis
* @date 2024-02-17 22:15
*/
public function listen(Request $request)
{
$where = $this->validate(LogSearchValidate::class);
if ($request->header('accept') == 'text/event-stream') {
$connection = $request->connection;
$connection->send(new Response(200, ['Content-Type' => $request->header('accept')], "\r\n"));

if(!empty($where['content'])) {
$words = $this->logService->wordSegmentation($where['content']);
$connection->send(new ServerSentEvents([
'event' => 'message',
'data' => json_encode([
'type' => 'words',
'data' => $words
])
]));
}


$where['id'] = 0;
// 定时向客户端推送数据
$timerId = Timer::add(2, function () use ($connection, &$timerId, &$where, &$lastId) {
// 连接关闭的时候要将定时器删除,避免定时器不断累积导致内存泄漏
if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
Timer::del($timerId);
return;
}

$data = $this->logService->search($where);
if (!$data->isEmpty()) {
$where['id'] = $data->first()->id + 1;
// 发送message事件,事件携带的数据为日志列表
$connection->send(new ServerSentEvents([
'event' => 'message',
'data' => json_encode([
'type' => 'logs',
'data' => $data
])
]));
}
});
return;
}
return error('请求类型错误');
}
}
26 changes: 26 additions & 0 deletions app/exception/BusinessException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace app\exception;

use support\exception\BusinessException as BaseBusinessException;
use Webman\Http\Request;
use Webman\Http\Response;

class BusinessException extends BaseBusinessException
{
protected $code = 1;
protected $message = '业务异常';
protected $data = [];

public function __construct($message = '', $code = 1, $data = [])
{
$this->message = $message;
$this->code = $code;
$this->data = $data;
}

public function render(Request $request): ?Response
{
return error($this->message, $this->code, $this->data);
}
}
Loading

0 comments on commit ae65f1e

Please sign in to comment.