Skip to content

Commit 1965138

Browse files
committed
Add a custom monolog formatter for Nutgram
1 parent 368884f commit 1965138

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="mb-1">
2+
<div>
3+
<span class="px-1 bg-yellow-500 text-black">{{$time}}</span>
4+
<span class="mx-1">{{$message}}</span>
5+
<span class="px-1 bg-green-500 text-black">{{$endpoint}}</span>
6+
</div>
7+
<div class="text-gray-500">{{$content}}</div>
8+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="mb-1">
2+
<div>
3+
<span class="px-1 bg-yellow-500 text-black">{{$time}}</span>
4+
<span class="mx-1">{{$message}}</span>
5+
</div>
6+
<div class="text-gray-500">{{$response}}</div>
7+
<div class="content-repeat-['.']"></div>
8+
</div>

src/Log/NutgramFormatter.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Nutgram\Laravel\Log;
4+
5+
use Monolog\Formatter\FormatterInterface;
6+
use Monolog\LogRecord;
7+
use function Termwind\{render};
8+
9+
class NutgramFormatter implements FormatterInterface
10+
{
11+
public function format(LogRecord $record): void
12+
{
13+
$record->context['type'] === 'request' ? $this->formatRequest($record) : $this->formatResponse($record);
14+
}
15+
16+
public function formatBatch(array $records): void
17+
{
18+
array_walk($records, [$this, 'format']);
19+
}
20+
21+
public function formatRequest(LogRecord $record): void
22+
{
23+
$content = json_encode($record->context['content'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
24+
25+
render(view('logging::request', [
26+
'time' => $record->datetime->format('Y-m-d H:i:s'),
27+
'message' => $record->message,
28+
'endpoint' => $record->context['endpoint'],
29+
'content' => $content,
30+
]));
31+
}
32+
33+
public function formatResponse(LogRecord $record): void
34+
{
35+
$response = json_encode($record->context['response'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
36+
37+
render(view('logging::response', [
38+
'time' => $record->datetime->format('Y-m-d H:i:s'),
39+
'message' => $record->message,
40+
'response' => $response,
41+
]));
42+
}
43+
}

src/NutgramServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function boot(): void
8686
{
8787
if ($this->app->runningInConsole()) {
8888
$this->loadViewsFrom(__DIR__.'/../resources/views/terminal', 'terminal');
89+
$this->loadViewsFrom(__DIR__.'/../resources/views/logging', 'logging');
8990

9091
$this->commands([
9192
Console\RunCommand::class,

0 commit comments

Comments
 (0)