Skip to content

Commit bafc891

Browse files
authored
Update Laravel DevLogger for Laravel 12 compatibility
1 parent c39014a commit bafc891

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
}
2828
},
2929
"require": {
30-
"php": "^8.0",
31-
"illuminate/support": "^9.0|^10.0|^11.0",
32-
"illuminate/database": "^9.0|^10.0|^11.0",
33-
"illuminate/contracts": "^9.0|^10.0|^11.0"
30+
"php": "^8.2",
31+
"illuminate/support": "^9.0|^10.0|^11.0|^12.0",
32+
"illuminate/database": "^9.0|^10.0|^11.0|^12.0",
33+
"illuminate/contracts": "^9.0|^10.0|^11.0|^12.0"
3434
},
3535
"require-dev": {
36-
"orchestra/testbench": "^7.0|^8.0|^9.0",
37-
"phpunit/phpunit": "^9.0|^10.0"
36+
"orchestra/testbench": "^7.0|^8.0|^9.0|^10.0",
37+
"phpunit/phpunit": "^9.0|^10.0|^11.0"
3838
},
3939
"minimum-stability": "stable",
4040
"prefer-stable": true

migrations/2024_01_01_000000_create_developer_logs_table.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
public function up(): void
1313
{
1414
$tableName = config('devlogger.table_name', 'developer_logs');
15-
$connection = config('devlogger.database_connection');
15+
$connection = config('devlogger.database_connection', null);
1616

17-
Schema::connection($connection)->create($tableName, function (Blueprint $table) {
17+
$schema = $connection ? Schema::connection($connection) : Schema::connection();
18+
19+
$schema->create($tableName, function (Blueprint $table) {
1820
$table->id();
1921
$table->string('level')->index();
2022
$table->string('queue')->nullable()->index();
@@ -48,8 +50,10 @@ public function up(): void
4850
public function down(): void
4951
{
5052
$tableName = config('devlogger.table_name', 'developer_logs');
51-
$connection = config('devlogger.database_connection');
53+
$connection = config('devlogger.database_connection', null);
5254

53-
Schema::connection($connection)->dropIfExists($tableName);
55+
$schema = $connection ? Schema::connection($connection) : Schema::connection();
56+
57+
$schema->dropIfExists($tableName);
5458
}
5559
};

src/Models/DeveloperLog.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\SoftDeletes;
88
use Illuminate\Database\Eloquent\Builder;
99
use Carbon\Carbon;
10+
use Illuminate\Database\Eloquent\Casts\Attribute;
1011

1112
class DeveloperLog extends Model
1213
{
@@ -133,26 +134,34 @@ public function removeTags(array $tags): bool
133134

134135
/**
135136
* Get formatted context for display
137+
*
138+
* @return Attribute
136139
*/
137-
public function getFormattedContextAttribute(): string
140+
protected function formattedContext(): Attribute
138141
{
139-
if (empty($this->context)) {
140-
return '';
141-
}
142-
143-
return json_encode($this->context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
142+
return Attribute::make(
143+
get: fn () => empty($this->context)
144+
? ''
145+
: json_encode($this->context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
146+
);
144147
}
145148

146149
/**
147150
* Get short file path (relative to project root)
151+
*
152+
* @return Attribute
148153
*/
149-
public function getShortFilePathAttribute(): string
154+
protected function shortFilePath(): Attribute
150155
{
151-
if (empty($this->file_path)) {
152-
return 'unknown';
153-
}
154-
155-
$basePath = base_path();
156-
return str_replace($basePath . '/', '', $this->file_path);
156+
return Attribute::make(
157+
get: function () {
158+
if (empty($this->file_path)) {
159+
return 'unknown';
160+
}
161+
162+
$basePath = base_path();
163+
return str_replace($basePath . '/', '', $this->file_path);
164+
}
165+
);
157166
}
158167
}

src/Services/DeveloperLogService.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use DevLoggerPackage\Models\DeveloperLog;
66
use Illuminate\Support\Facades\Log;
7-
use Illuminate\Support\Facades\Request;
7+
use Illuminate\Http\Request;
88
use Illuminate\Support\Facades\Auth;
99
use Throwable;
1010

@@ -167,11 +167,11 @@ protected function prepareLogData(string $level, string $message, array $context
167167
'line_number' => $callerInfo['line'],
168168
'queue' => $this->queue,
169169
'tags' => !empty($this->tags) ? $this->tags : null,
170-
'request_url' => Request::fullUrl(),
171-
'request_method' => Request::method(),
170+
'request_url' => request()->fullUrl(),
171+
'request_method' => request()->method(),
172172
'user_id' => Auth::id(),
173-
'ip_address' => Request::ip(),
174-
'user_agent' => Request::userAgent(),
173+
'ip_address' => request()->ip(),
174+
'user_agent' => request()->userAgent(),
175175
'status' => 'open',
176176
];
177177
}
@@ -194,11 +194,11 @@ protected function prepareExceptionLogData(Throwable $exception, array $context)
194194
'stack_trace' => $exception->getTraceAsString(),
195195
'queue' => $this->queue,
196196
'tags' => !empty($this->tags) ? array_merge($this->tags, ['exception']) : ['exception'],
197-
'request_url' => Request::fullUrl(),
198-
'request_method' => Request::method(),
197+
'request_url' => request()->fullUrl(),
198+
'request_method' => request()->method(),
199199
'user_id' => Auth::id(),
200-
'ip_address' => Request::ip(),
201-
'user_agent' => Request::userAgent(),
200+
'ip_address' => request()->ip(),
201+
'user_agent' => request()->userAgent(),
202202
'status' => 'open',
203203
];
204204
}

0 commit comments

Comments
 (0)