Skip to content

Commit 7aab176

Browse files
authored
Merge pull request #11 from BinarCode/2.x
2.x
2 parents afc82e8 + e6e2f6d commit 7aab176

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+766
-167
lines changed

.github/workflows/php-cs-fixer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Run PHP CS Fixer
1616
uses: docker://oskarstark/php-cs-fixer-ga
1717
with:
18-
args: --config=.php_cs.dist --allow-risky=yes
18+
args: --config=.php_cs.dist.php --allow-risky=yes
1919

2020
- name: Commit changes
2121
uses: stefanzweifel/git-auto-commit-action@v4

.github/workflows/psalm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup PHP
1717
uses: shivammathur/setup-php@v2
1818
with:
19-
php-version: '7.4'
19+
php-version: '8.0'
2020
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
2121
coverage: none
2222

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.0, 7.4]
12+
php: [8.0]
1313
laravel: [8.*]
1414
stability: [prefer-stable]
1515
include:

.php-cs-fixer.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"php":"8.0.11","version":"3.2.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Commands\/PruneCommand.php":1473367127,"src\/Telescope\/TelescopeException.php":1871046959,"src\/Telescope\/TelescopeDev.php":2309110818,"src\/LaravelDeveloper.php":1129170780,"src\/Dtos\/DevLogDto.php":1643551788,"src\/Dtos\/DevNotificationDto.php":412948095,"src\/Notifications\/DevNotification.php":2947327537,"src\/Notifications\/Slack.php":3934215647,"src\/Middleware\/DevSanctumAuthMiddleware.php":964085018,"src\/Middleware\/DevAuthMiddleware.php":1511410281,"src\/Models\/DeveloperLog.php":4104297917,"src\/Models\/Concerns\/WithUuid.php":1102884493,"src\/Models\/Concerns\/WithCreator.php":2183877251,"src\/Models\/Concerns\/InteractsWithDeveloper.php":1790705050,"src\/LaravelDeveloperServiceProvider.php":1956646152,"src\/Profiling\/ServerMemory.php":1460695193,"src\/Profiling\/ServerTiming.php":4274056244,"src\/helpers.php":715232250,"src\/Concerns\/Proxies.php":524787727,"src\/Concerns\/ClassResolver.php":1880027722,"src\/LaravelDeveloperFacade.php":3477645298,"src\/Nova\/DeveloperLogResource.php":9639465,"src\/Nova\/Fields\/Line.php":2270391664,"src\/Nova\/Fields\/CreatedAt.php":2148384209,"src\/Nova\/Fields\/Badge.php":1570922008,"src\/Nova\/Fields\/ExpirationDateBadge.php":4282897254,"src\/Nova\/Fields\/BelongsTo.php":1822753998,"src\/Nova\/Fields\/BadgeList.php":3076744754,"src\/Nova\/Fields\/DangerBadge.php":3216791755,"src\/Nova\/Filters\/EndingAtFilter.php":2850496008,"src\/Nova\/Filters\/HasResultsFilter.php":4116547610,"src\/Nova\/Filters\/FromDateFilter.php":1053058278,"src\/Nova\/Filters\/ToDateFilter.php":581993863,"src\/Nova\/Filters\/CreatedAtFilter.php":778019993,"src\/Nova\/Filters\/TagFilter.php":1978666758,"tests\/TestCase.php":3638931094,"tests\/Fixtures\/DummyNotification.php":3913031193,"tests\/Fixtures\/User.php":353028011,"tests\/Notifications\/DevNotificationTest.php":3587569263,"tests\/Middleware\/DevAuthMiddlewareTest.php":1606798965,"tests\/Middleware\/DevSanctumAuthMiddlewareTest.php":3421724279,"tests\/ExampleTest.php":1575370042,"tests\/Helpers\/SlackHelperTest.php":1334877550,"tests\/Helpers\/DevLogHelperTest.php":585022422,"tests\/Models\/ExceptionLogTest.php":2235509697,"tests\/Models\/LaravelDeveloperTest.php":1546634681,"tests\/Console\/PruneCommandTest.php":4199024702,"tests\/Mock\/CustomNotificationMock.php":2052176798,"tests\/Mock\/PayloadMock.php":1887690153,"tests\/Profiling\/ServerTimingTest.php":1295567184,"tests\/Profiling\/ServerMemoryTest.php":4137425435,"tests\/Mock.php":3663171411}}

.php_cs.dist renamed to .php_cs.dist.php

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

33
$finder = Symfony\Component\Finder\Finder::create()
4-
->notPath('bootstrap/*')
5-
->notPath('storage/*')
6-
->notPath('resources/view/mail/*')
74
->in([
85
__DIR__ . '/src',
96
__DIR__ . '/tests',
@@ -13,14 +10,14 @@
1310
->ignoreDotFiles(true)
1411
->ignoreVCS(true);
1512

16-
return PhpCsFixer\Config::create()
13+
return (new PhpCsFixer\Config())
1714
->setRules([
18-
'@PSR2' => true,
15+
'@PSR12' => true,
1916
'array_syntax' => ['syntax' => 'short'],
20-
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
17+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
2118
'no_unused_imports' => true,
2219
'not_operator_with_successor_space' => true,
23-
'trailing_comma_in_multiline_array' => true,
20+
'trailing_comma_in_multiline' => true,
2421
'phpdoc_scalar' => true,
2522
'unary_operator_spaces' => true,
2623
'binary_operator_spaces' => true,
@@ -31,7 +28,7 @@
3128
'phpdoc_var_without_name' => true,
3229
'class_attributes_separation' => [
3330
'elements' => [
34-
'method',
31+
'method' => 'one',
3532
],
3633
],
3734
'method_argument_space' => [

README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ return [
5050
*
5151
* We will replace the uuid with the exception log uuid.
5252
*/
53-
'exception_log_base_url' => env('DEV_EXCEPTION_LOG_BASE_URL'),
53+
'developer_log_base_url' => env('DEV_developer_log_base_url'),
5454

5555
/**
5656
* The default notification class used to send notifications.
@@ -110,15 +110,15 @@ LaravelDeveloper::toDevSlack(
110110

111111
### Persist exception
112112

113-
If you want to persist the exception into the database, in any place you want to catch and log an exception, you can do something like this:
113+
If you want to persist the exception into the database, in any place you want to catch and log an exception, you can do something like this:
114114

115115
```php
116-
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
116+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
117117

118118
try {
119119
// Your custom code
120120
} catch (\Throwable $e) {
121-
ExceptionLog::makeFromException($e)->save();
121+
DeveloperLog::makeFromException($e)->save();
122122
}
123123
```
124124

@@ -143,12 +143,12 @@ You can specify payload to your exception, so it will be stored along with the e
143143

144144
```php
145145
use Laravel\Cashier\Exceptions\PaymentFailure;
146-
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
146+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
147147

148148
try {
149149
// Your custom code
150150
} catch (PaymentFailure $e) {
151-
ExceptionLog::makeFromException($e, $e->payment->asStripePaymentIntent())->notifyDevs();
151+
DeveloperLog::makeFromException($e, $e->payment->asStripePaymentIntent())->notifyDevs();
152152
}
153153
```
154154

@@ -369,6 +369,32 @@ devLog('Called Fedex API with:', $payload)
369369

370370
Now you will have an entry in your database table with the payload and the associated name.
371371

372+
### Attach target
373+
374+
You can attach a target to your log, so it will relate to a model like this:
375+
376+
```php
377+
devLog('Model updated')->target($user);
378+
```
379+
380+
Or if you need to attach many models, use:
381+
382+
```php
383+
devLog('Targetable')
384+
->addRelatedModel($user)
385+
->addRelatedModel($post)
386+
->save();
387+
```
388+
389+
### Attach meta
390+
391+
You also can attach meta data to your logs:
392+
393+
```php
394+
devLog('Targetable')
395+
->addMeta(['browser' => 'safari',])
396+
->save();
397+
```
372398

373399
## Telescope support
374400

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.4|^8.0",
19+
"php": "^8.0",
2020
"illuminate/contracts": "^8.0",
2121
"illuminate/support": "^7.0|^8.0",
2222
"laravel/slack-notification-channel": "^2.3",
2323
"ext-json": "*"
2424
},
2525
"require-dev": {
26+
"friendsofphp/php-cs-fixer": "^3.2",
2627
"laravel/sanctum": "^2.8",
2728
"orchestra/testbench": "^6.0",
2829
"phpunit/phpunit": "^9.3",

config/developer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<?php
22

33
return [
4+
/**
5+
* The database table name.
6+
*/
7+
'table' => 'developer_logs',
8+
9+
/**
10+
* The model used to manage logs.
11+
*/
12+
'model' => \Binarcode\LaravelDeveloper\Models\DeveloperLog::class,
13+
414
/**
515
* The slack incoming webhook to send notifications.
616
*/
@@ -13,7 +23,7 @@
1323
*
1424
* We will replace the uuid with the exception log uuid.
1525
*/
16-
'exception_log_base_url' => env('DEV_EXCEPTION_LOG_BASE_URL'),
26+
'developer_log_base_url' => env('DEVELOPER_LOG_BASE_URL'),
1727

1828
/**
1929
* The default notification class used to send notifications.

database/factories/ExceptionLogFactory.php renamed to database/factories/DeveloperLogFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Binarcode\LaravelDeveloper\Database\Factories;
44

5-
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
5+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
66
use Illuminate\Database\Eloquent\Factories\Factory;
77

8-
class ExceptionLogFactory extends Factory
8+
class DeveloperLogFactory extends Factory
99
{
10-
protected $model = ExceptionLog::class;
10+
protected $model = DeveloperLog::class;
1111

12-
public function definition()
12+
public function definition(): array
1313
{
1414
return [
1515
'name' => $this->faker->text(10),

database/migrations/create_laravel_developer_table.php.stub

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ class CreateLaravelDeveloperTable extends Migration
88
{
99
public function up()
1010
{
11-
Schema::create('exception_logs', function (Blueprint $table) {
11+
$table = config('developer.table') ?? 'developer_logs';
12+
13+
Schema::create($table, function (Blueprint $table) {
1214
$table->id();
1315
$table->uuid('uuid');
1416

1517
$table->string('name')->nullable();
1618
$table->string('tags')->nullable();
17-
$table->text('payload')->nullable();
19+
$table->json('payload')->nullable();
1820
$table->text('exception')->nullable();
1921
$table->text('previous')->nullable();
2022
$table->string('file')->nullable();
2123
$table->string('line')->nullable();
2224
$table->string('code')->nullable();
25+
$table->string('target_type')->nullable();
26+
$table->unsignedBigInteger('target_id')->nullable();
27+
$table->json('related_models')->nullable();
28+
$table->json('meta')->nullable();
2329

2430
$table->bigInteger('created_by')->nullable();
2531
$table->timestamps();
@@ -28,6 +34,8 @@ class CreateLaravelDeveloperTable extends Migration
2834

2935
public function down()
3036
{
31-
Schema::dropIfExists('exception_logs');
37+
$table = config('developer.table') ?? 'developer_logs';
38+
39+
Schema::dropIfExists($table);
3240
}
3341
}

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<directory name="src"/>
1212
<ignoreFiles>
1313
<directory name="vendor"/>
14+
<directory name="src/Nova"/>
15+
<directory name="src/Telescope"/>
1416
</ignoreFiles>
1517
</projectFiles>
1618
</psalm>

src/Commands/PruneCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Binarcode\LaravelDeveloper\Commands;
44

5-
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
5+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
66
use Illuminate\Console\Command;
77

88
class PruneCommand extends Command
@@ -13,6 +13,6 @@ class PruneCommand extends Command
1313

1414
public function handle()
1515
{
16-
$this->info(ExceptionLog::prune(now()->subHours((int) $this->option('hours'))) . ' entries pruned.');
16+
$this->info(DeveloperLog::prune(now()->subHours((int) $this->option('hours'))) . ' entries pruned.');
1717
}
1818
}

src/Concerns/ClassResolver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Concerns;
4+
5+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
6+
7+
trait ClassResolver
8+
{
9+
public static function model(): string
10+
{
11+
return config('developer.model') ?? DeveloperLog::class;
12+
}
13+
}

src/Dtos/DevLogDto.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Dtos;
4+
5+
use Binarcode\LaravelDeveloper\Models\DeveloperLog;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Collection;
8+
9+
class DevLogDto
10+
{
11+
public string $name;
12+
13+
public array $payload = [];
14+
15+
public ?string $tags;
16+
17+
public ?Model $target = null;
18+
19+
public ?Collection $relatedModels;
20+
21+
public ?Collection $meta;
22+
23+
public function __construct(string $name = 'Dev Log', ?array $payload = [], ?string $tags = null)
24+
{
25+
$this->payload = $payload ?? [];
26+
$this->name = $name;
27+
$this->tags = $tags;
28+
$this->relatedModels = collect();
29+
$this->meta = collect();
30+
}
31+
32+
public function setPayload(array $payload): self
33+
{
34+
$this->payload = $payload;
35+
36+
return $this;
37+
}
38+
39+
public function setName(string $name): self
40+
{
41+
$this->name = $name;
42+
43+
return $this;
44+
}
45+
46+
public function setTags(string $tags): self
47+
{
48+
$this->tags = $tags;
49+
50+
return $this;
51+
}
52+
53+
public static function make(...$args): self
54+
{
55+
return new static(...$args);
56+
}
57+
58+
public function getPayload(): array
59+
{
60+
return $this->payload;
61+
}
62+
63+
public function target(Model $model): self
64+
{
65+
$this->target = $model;
66+
67+
return $this;
68+
}
69+
70+
public function addRelatedModel(Model $model): self
71+
{
72+
$this->relatedModels->push([
73+
'target_id' => $model->getKey(),
74+
'target_type' => $model->getMorphClass(),
75+
]);
76+
77+
return $this;
78+
}
79+
80+
public function addMeta(array $meta): self
81+
{
82+
$this->meta->push($meta);
83+
84+
return $this;
85+
}
86+
87+
public function save(): void
88+
{
89+
DeveloperLog::makeFromDevLog($this)->save();
90+
}
91+
92+
public function __destruct()
93+
{
94+
$this->save();
95+
}
96+
}

0 commit comments

Comments
 (0)