Skip to content

Commit 17671d1

Browse files
author
miladimos
committed
Add simple logger service
1 parent 4ea1d90 commit 17671d1

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

config/filemanager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@
150150

151151
'logger' => [
152152
'active' => false,
153-
'driver' => ''
153+
'channel' => env("LOG_CHANNEL", "stack"),
154+
'level' => 'debug', // default level
154155
],
155156

156157
/**

src/Services/LoggerService.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Miladimos\FileManager\Services;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use function PHPUnit\Framework\throwException;
7+
8+
class LoggerService
9+
{
10+
private $channel;
11+
12+
public function __construct()
13+
{
14+
$this->channel = config('filemanager.logger.channel');
15+
}
16+
17+
public function log($msg, $level = 'debug')
18+
{
19+
if (!$this->checkLevel($level)) return false;
20+
21+
Log::channel($this->channel)->$level($msg);
22+
}
23+
24+
private function checkLevel($level)
25+
{
26+
$levels = ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'];
27+
return in_array($level, $levels);
28+
}
29+
}

src/Services/Service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ abstract class Service
2727

2828
protected $ds = DIRECTORY_SEPARATOR;
2929

30+
protected $loggerService;
31+
3032
public function __construct()
3133
{
3234
$this->disk = Storage::disk(config('filemanager.disk'));
3335
$this->disk_name = config('filemanager.disk');
3436
$this->base_directory = config('filemanager.base_directory');
3537
$this->mimeDetect = new FinfoMimeTypeDetector();
38+
$this->loggerService = new LoggerService();
3639
}
3740

3841
/**

src/Services/UploadService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Illuminate\Support\Facades\Storage;
1212
use Illuminate\Support\Str;
1313
use Intervention\Image\Facades\Image;
14+
use Miladimos\FileManager\Events\AfterUpload;
15+
use Miladimos\FileManager\Events\BeforeUpload;
1416
use Miladimos\FileManager\Models\Directory;
1517
use Miladimos\FileManager\Models\File;
1618

@@ -111,6 +113,8 @@ public function uploadImage(UploadedFile $uploadedFile, $directory_id = 0)
111113

112114
$fullUploadedPath = $path . $this->ds . $finalFileName;
113115

116+
event(new BeforeUpload());
117+
114118
if ($this->disk->put($fullUploadedPath, $image->encode())) {
115119
DB::transaction(function () use ($originalName, $finalFileName, $directory_id, $path, $fullUploadedPath, $fileSize, $mimeType, $fileExt, $image) {
116120
$this->fileModel->create([
@@ -129,10 +133,12 @@ public function uploadImage(UploadedFile $uploadedFile, $directory_id = 0)
129133
]);
130134
});
131135

136+
event(new AfterUpload());
137+
132138
return true;
133-
} else
139+
} else {
134140
return false;
135-
141+
}
136142
}
137143

138144
return false;

src/helpers.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ function version(): string
9292
if (!function_exists('checkInstanceOf')) {
9393
function checkInstanceOf($varable, string $model): string
9494
{
95-
9695
return !!($varable instanceof $model);
9796
}
9897
}
@@ -104,4 +103,11 @@ function generateDownloadHash(): string
104103
}
105104
}
106105

106+
if (!function_exists('logActive')) {
107+
function logActive(): bool
108+
{
109+
return config('filemanager.logger.active');
110+
}
111+
}
112+
107113

0 commit comments

Comments
 (0)