-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a custom monolog formatter for Nutgram
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="mb-1"> | ||
<div> | ||
<span class="px-1 bg-yellow-500 text-black">{{$time}}</span> | ||
<span class="mx-1">{{$message}}</span> | ||
<span class="px-1 bg-green-500 text-black">{{$endpoint}}</span> | ||
</div> | ||
<div class="text-gray-500">{{$content}}</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="mb-1"> | ||
<div> | ||
<span class="px-1 bg-yellow-500 text-black">{{$time}}</span> | ||
<span class="mx-1">{{$message}}</span> | ||
</div> | ||
<div class="text-gray-500">{{$response}}</div> | ||
<div class="content-repeat-['.']"></div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Nutgram\Laravel\Log; | ||
|
||
use Monolog\Formatter\FormatterInterface; | ||
use Monolog\LogRecord; | ||
use function Termwind\{render}; | ||
|
||
class NutgramFormatter implements FormatterInterface | ||
{ | ||
public function format(LogRecord $record): void | ||
{ | ||
$record->context['type'] === 'request' ? $this->formatRequest($record) : $this->formatResponse($record); | ||
} | ||
|
||
public function formatBatch(array $records): void | ||
{ | ||
array_walk($records, [$this, 'format']); | ||
} | ||
|
||
public function formatRequest(LogRecord $record): void | ||
{ | ||
$content = json_encode($record->context['content'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | ||
|
||
render(view('logging::request', [ | ||
'time' => $record->datetime->format('Y-m-d H:i:s'), | ||
'message' => $record->message, | ||
'endpoint' => $record->context['endpoint'], | ||
'content' => $content, | ||
])); | ||
} | ||
|
||
public function formatResponse(LogRecord $record): void | ||
{ | ||
$response = json_encode($record->context['response'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | ||
|
||
render(view('logging::response', [ | ||
'time' => $record->datetime->format('Y-m-d H:i:s'), | ||
'message' => $record->message, | ||
'response' => $response, | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters