mirror of
https://github.com/v2board/v2board.git
synced 2025-06-15 22:27:50 +08:00
update: log api
This commit is contained in:
11
app/Logging/MysqlLogger.php
Normal file
11
app/Logging/MysqlLogger.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace App\Logging;
|
||||
|
||||
class MysqlLogger
|
||||
{
|
||||
public function __invoke(array $config){
|
||||
return tap(new \Monolog\Logger('mysql'), function ($logger) {
|
||||
$logger->pushHandler(new MysqlLoggerHandler());
|
||||
});
|
||||
}
|
||||
}
|
44
app/Logging/MysqlLoggerHandler.php
Normal file
44
app/Logging/MysqlLoggerHandler.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace App\Logging;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Logger;
|
||||
use App\Models\Log as LogModel;
|
||||
|
||||
class MysqlLoggerHandler extends AbstractProcessingHandler
|
||||
{
|
||||
public function __construct($level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
}
|
||||
|
||||
protected function write(array $record): void
|
||||
{
|
||||
try{
|
||||
if(isset($record['context']['exception']) && is_object($record['context']['exception'])){
|
||||
$record['context']['exception'] = (array)$record['context']['exception'];
|
||||
}
|
||||
$record['request_data'] = request()->all() ??[];
|
||||
$log = [
|
||||
'title' => $record['message'],
|
||||
'level' => $record['level_name'],
|
||||
'host' => $record['request_host'] ?? request()->getSchemeAndHttpHost(),
|
||||
'uri' => $record['request_uri'] ?? request()->getRequestUri(),
|
||||
'method' => $record['request_method'] ?? request()->getMethod(),
|
||||
'ip' => request()->getClientIp(),
|
||||
'data' => json_encode($record['request_data']) ,
|
||||
'context' => isset($record['context']) ? json_encode($record['context']) : '',
|
||||
'created_at' => strtotime($record['datetime']),
|
||||
'updated_at' => strtotime($record['datetime']),
|
||||
];
|
||||
|
||||
LogModel::insert(
|
||||
$log
|
||||
);
|
||||
}catch (\Exception $e){
|
||||
Log::channel('daily')->error($e->getMessage().$e->getFile().$e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user