Skip to content

Commit 62743e0

Browse files
committed
Adding a way to store logs.
1 parent 25c6061 commit 62743e0

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

src/LaravelDeveloper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ public static function notifyUsing(?callable $notificator)
8080
{
8181
Slack::$notifyUsing = $notificator;
8282
}
83+
84+
public static function log()
85+
{
86+
87+
}
8388
}

src/Models/ExceptionLog.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binarcode\LaravelDeveloper\LaravelDeveloper;
66
use Binarcode\LaravelDeveloper\Models\Concerns\WithCreator;
77
use Binarcode\LaravelDeveloper\Models\Concerns\WithUuid;
8+
use Binarcode\LaravelDeveloper\Notifications\DevLog;
89
use DateTimeInterface;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
1011
use Illuminate\Database\Eloquent\Model;
@@ -51,6 +52,14 @@ public function getKeyName()
5152
return 'uuid';
5253
}
5354

55+
public static function makeFromDevLog(DevLog $log): self
56+
{
57+
return new static([
58+
'name' => $log->name,
59+
'payload' => $log->payload
60+
]);
61+
}
62+
5463
public static function makeFromException(Throwable $throwable, JsonSerializable $payload = null): self
5564
{
5665
return new static([

src/Notifications/DevLog.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Notifications;
4+
5+
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
6+
7+
class DevLog
8+
{
9+
public array $payload;
10+
11+
public string $name;
12+
13+
public function __construct(string $name = 'Dev Log', array $payload = [])
14+
{
15+
$this->payload = $payload;
16+
$this->name = $name;
17+
}
18+
19+
public function setPayload(array $payload): self
20+
{
21+
$this->payload = $payload;
22+
23+
return $this;
24+
}
25+
26+
public function setName(string $name): self
27+
{
28+
$this->name = $name;
29+
30+
return $this;
31+
}
32+
33+
public static function make(string $name = 'Dev Log', array $payload = null): self
34+
{
35+
return new static($name, $payload);
36+
}
37+
38+
public function getPayload(): array
39+
{
40+
return $this->payload;
41+
}
42+
43+
public function __destruct()
44+
{
45+
ExceptionLog::makeFromDevLog($this)->save();
46+
}
47+
48+
}

src/helpers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Binarcode\LaravelDeveloper\Notifications\DevLog;
34
use Binarcode\LaravelDeveloper\Notifications\Slack;
45
use Binarcode\LaravelDeveloper\Profiling\ServerMemory;
56
use Binarcode\LaravelDeveloper\Profiling\ServerTiming;
@@ -46,3 +47,10 @@ function slack(...$args)
4647
return Slack::make($args);
4748
}
4849
}
50+
51+
if (! function_exists('devLog')) {
52+
function devLog(...$args): DevLog
53+
{
54+
return DevLog::make(...$args);
55+
}
56+
}

tests/Helpers/DevLogHelperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Tests\Helpers;
4+
5+
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
6+
use Binarcode\LaravelDeveloper\Notifications\DevLog;
7+
use Binarcode\LaravelDeveloper\Tests\TestCase;
8+
9+
class DevLogHelperTest extends TestCase
10+
{
11+
public function test_dev_log_can_store_payload(): void
12+
{
13+
$payload = ['a' => 'b'];
14+
15+
$this->assertInstanceOf(DevLog::class, $log = devLog('Dev Log', $payload));
16+
17+
$log->__destruct();
18+
19+
$this->assertDatabaseHas('exception_logs', [
20+
'name' => 'Dev Log',
21+
]);
22+
23+
$exceptionLog = ExceptionLog::first();
24+
25+
$this->assertSame(
26+
$payload,
27+
$exceptionLog->payload
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)