added get options
This commit is contained in:
@@ -18,43 +18,110 @@ class LogController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*
|
*
|
||||||
* @param Request $request The log request.
|
* @param Request $request The log request.
|
||||||
|
* @param string $name The log name.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function show(Request $request, string $name)
|
public function show(Request $request, string $name)
|
||||||
{
|
{
|
||||||
if($request->user()?->hasPermission('logs/' . $name)) {
|
if ($request->user()?->hasPermission('logs/' . $name) === true) {
|
||||||
switch(strtolower($name)) {
|
switch (strtolower($name)) {
|
||||||
case 'discord':
|
case 'discord':
|
||||||
$outputContents = '';
|
$data = [];
|
||||||
$errorContents = '';
|
// $outputContents = '';
|
||||||
|
// $errorContents = '';
|
||||||
|
|
||||||
// output log
|
$logs = $request->get('logs');
|
||||||
$filePath = '/home/discordbot/.pm2/logs/stemmech-discordbot-out-0.log';
|
if ($logs === null) {
|
||||||
if(file_exists($filePath) === true) {
|
$logs = ['output', 'error'];
|
||||||
$outputContents = file_get_contents($filePath);
|
} else {
|
||||||
|
$logs = explode(',', strtolower($logs));
|
||||||
}
|
}
|
||||||
|
|
||||||
$lines = preg_split("/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: (?:(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: )[\s\S])*)/", $outputContents, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
$lines = intval($request->get('lines', 50));
|
||||||
$outputContents = implode('', array_reverse($lines));
|
if ($lines > 100) {
|
||||||
|
$lines = 100;
|
||||||
// error log
|
} elseif ($lines < 0) {
|
||||||
$filePath = '/home/discordbot/.pm2/logs/stemmech-discordbot-error-0.log';
|
$lines = 1;
|
||||||
if(file_exists($filePath) === true) {
|
|
||||||
$errorContents = file_get_contents($filePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$lines = preg_split("/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: (?:(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: )[\s\S])*)/", $errorContents, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
$before = $request->get('before');
|
||||||
$errorContents = implode('', array_reverse($lines));
|
if ($before !== null) {
|
||||||
|
$before = preg_split("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}): /", $before, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($before) < 6) {
|
||||||
|
$before = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$after = $request->get('after');
|
||||||
|
if ($after !== null) {
|
||||||
|
$after = preg_split("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}): /", $after, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($after) < 6) {
|
||||||
|
$after = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$logFiles = [
|
||||||
|
[
|
||||||
|
'name' => 'output',
|
||||||
|
'path' => '/home/discordbot/.pm2/logs/stemmech-discordbot-out-0.log'
|
||||||
|
],[
|
||||||
|
'name' => 'error',
|
||||||
|
'path' => '/home/discordbot/.pm2/logs/stemmech-discordbot-error-0.log'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($logFiles as $logFile) {
|
||||||
|
if (in_array($logFile['name'], $logs) === true) {
|
||||||
|
$logContent = '';
|
||||||
|
|
||||||
|
if (file_exists($logFile['path']) === true) {
|
||||||
|
$logContent = file_get_contents($logFile['path']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$logArray = preg_split("/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: (?:(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: )[\s\S])*)/", $logContent, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
|
||||||
|
$logContent = '';
|
||||||
|
$logLineCount = 0;
|
||||||
|
$logLineSkip = false;
|
||||||
|
foreach (array_reverse($logArray) as $logLine) {
|
||||||
|
$lineDate = preg_split("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}): /", $logLine, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($lineDate) >= 6) {
|
||||||
|
$logLineSkip = false;
|
||||||
|
|
||||||
|
// Is line before
|
||||||
|
if ($before !== null && ($lineDate[0] > $before[0] || $lineDate[1] > $before[1] || $lineDate[2] > $before[2] || $lineDate[3] > $before[3] || $lineDate[4] > $before[4] || $lineDate[5] > $before[5])) {
|
||||||
|
$logLineSkip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is line after
|
||||||
|
if ($after !== null && ($after[0] > $lineDate[0] || $after[1] > $lineDate[1] || $after[2] > $lineDate[2] || $after[3] > $lineDate[3] || $after[4] > $lineDate[4] || $after[5] > $lineDate[5])) {
|
||||||
|
$logLineSkip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$logLineCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($logLineCount > $lines) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($logLineSkip === false) {
|
||||||
|
$logContent .= $logLine;
|
||||||
|
}
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
|
$data[$logFile['name']] = $logContent;
|
||||||
|
}//end if
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
return $this->respondJson([
|
return $this->respondJson([
|
||||||
'log' => [
|
'log' => $data
|
||||||
'output' => $outputContents,
|
|
||||||
'errors' => $errorContents,
|
|
||||||
]
|
|
||||||
]);
|
]);
|
||||||
}
|
}//end switch
|
||||||
}
|
}//end if
|
||||||
|
|
||||||
return $this->respondForbidden();
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user