Skip to content

Commit 0b46202

Browse files
authored
Merge pull request #22 from spotlibs/feature/exploration
Logger with singleton context
2 parents 92324dd + c6ecb9e commit 0b46202

File tree

14 files changed

+311
-19
lines changed

14 files changed

+311
-19
lines changed

bootstrap/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,6 @@
9595
// ], function ($router) {
9696
// require __DIR__ . '/../routes/api.php';
9797
// });
98+
$app->register(\Spotlibs\PhpLib\Providers\ContextServiceProvider::class);
9899

99100
return $app;

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<exclude>
3131
<file>src/Exceptions/Handler.php</file>
3232
<directory suffix=".php">src/Commands</directory>
33+
<directory suffix=".php">src/Facades</directory>
3334
<directory suffix=".php">src/Libraries</directory>
3435
<directory suffix=".php">src/Middlewares</directory>
3536
<directory suffix=".php">src/Providers</directory>

src/Commands/Command.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8
5+
*
6+
* @category Library
7+
* @package Commands
8+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.3.1
11+
* @link https://github.com/spotlibs
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Spotlibs\PhpLib\Commands;
17+
18+
use Illuminate\Console\Command as BaseCommand;
19+
use Spotlibs\PhpLib\Services\Context;
20+
use Spotlibs\PhpLib\Services\Metadata;
21+
22+
/**
23+
* CommandInterface
24+
*
25+
* Command interface
26+
*
27+
* @category Console
28+
* @package Commands
29+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
30+
* @license https://mit-license.org/ MIT License
31+
* @link https://github.com/spotlibs
32+
*/
33+
34+
abstract class Command extends BaseCommand
35+
{
36+
public string $taskID;
37+
38+
/**
39+
* Creating instance
40+
*
41+
* @return self
42+
*/
43+
public function __construct()
44+
{
45+
parent::__construct();
46+
$this->taskID = uniqid() . '00000';
47+
$context = app(Context::class);
48+
$meta = new Metadata();
49+
$meta->task_id = $this->taskID;
50+
$meta->identifier = $this->signature;
51+
$context->set(Metadata::class, $meta);
52+
}
53+
}

src/Facades/Queue.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8
5+
*
6+
* @category Library
7+
* @package Facades
8+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.3.1
11+
* @link https://github.com/spotlibs
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Spotlibs\PhpLib\Facades;
17+
18+
use Illuminate\Support\Facades\Queue as BaseQueue;
19+
use Spotlibs\PhpLib\Services\Context;
20+
use Spotlibs\PhpLib\Services\Metadata;
21+
22+
/**
23+
* Queue
24+
*
25+
* Spolibs queue facades. Child of Illuminate\Support\Facades\Queue
26+
*
27+
* @category Helper
28+
* @package Facades
29+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
30+
* @license https://mit-license.org/ MIT License
31+
* @link https://github.com/spotlibs
32+
*/
33+
class Queue extends BaseQueue
34+
{
35+
/**
36+
* Push job to queue
37+
*
38+
* @param string $queue queue name
39+
* @param object|string $job instance of a job
40+
* @param mixed $data additional data (optional)
41+
*
42+
* @return mixed
43+
*/
44+
public static function pushOn(string $queue, object|string $job, mixed $data = ''): mixed
45+
{
46+
$context = app(Context::class);
47+
if ($context) {
48+
$meta = $context->get(Metadata::class);
49+
if ($meta instanceof Metadata) {
50+
if (isset($meta->req_id)) {
51+
$job->taskID = $meta->req_id;
52+
} elseif (isset($meta->task_id)) {
53+
$job->taskID = $meta->task_id;
54+
}
55+
$job->identifier = $meta->identifier;
56+
}
57+
}
58+
59+
return parent::pushOn($queue, $job, $data);
60+
}
61+
}

src/Logs/Log.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace Spotlibs\PhpLib\Logs;
1717

18+
use Spotlibs\PhpLib\Services\Context;
19+
1820
/**
1921
* Log
2022
*
@@ -33,7 +35,8 @@ class Log
3335
*/
3436
public static function activity(): Activity
3537
{
36-
return new Activity();
38+
$context = app(Context::class);
39+
return new Activity(context: $context);
3740
}
3841

3942
/**
@@ -43,7 +46,8 @@ public static function activity(): Activity
4346
*/
4447
public static function runtime(): Runtime
4548
{
46-
return new Runtime();
49+
$context = app(Context::class);
50+
return new Runtime($context);
4751
}
4852

4953
/**
@@ -53,6 +57,7 @@ public static function runtime(): Runtime
5357
*/
5458
public static function worker(): Worker
5559
{
56-
return new Worker();
60+
$context = app(Context::class);
61+
return new Worker($context);
5762
}
5863
}

src/Logs/Runtime.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Runtime
4141
*/
4242
public function warning(array $data)
4343
{
44+
$this->getEmbeddedInfo($data);
4445
BaseLog::channel($this->channel)->warning(json_encode($data));
4546
}
4647

@@ -53,6 +54,7 @@ public function warning(array $data)
5354
*/
5455
public function error(array $data)
5556
{
57+
$this->getEmbeddedInfo($data);
5658
BaseLog::channel($this->channel)->error(json_encode($data));
5759
}
5860
}

src/Logs/TraitLog.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace Spotlibs\PhpLib\Logs;
1717

1818
use Illuminate\Support\Facades\Log as BaseLog;
19+
use Spotlibs\PhpLib\Services\Context;
20+
use Spotlibs\PhpLib\Services\Metadata;
1921

2022
/**
2123
* TraitLog
@@ -28,6 +30,17 @@
2830
*/
2931
trait TraitLog
3032
{
33+
/**
34+
* Initiate Log
35+
*
36+
* @param Context $context context instance
37+
*
38+
* @return self
39+
*/
40+
public function __construct(protected Context $context)
41+
{
42+
}
43+
3144
/**
3245
* Logging with loglevel info
3346
*
@@ -37,6 +50,28 @@ trait TraitLog
3750
*/
3851
public function info(array $data)
3952
{
53+
$this->getEmbeddedInfo($data);
4054
BaseLog::channel($this->channel)->info(json_encode($data));
4155
}
56+
57+
/**
58+
* Embed basic info to data log
59+
*
60+
* @param array $data pointer of log data
61+
*
62+
* @return void
63+
*/
64+
private function getEmbeddedInfo(array &$data): void
65+
{
66+
if ($meta = $this->context->get(Metadata::class)) {
67+
if ($meta instanceof Metadata) {
68+
if (isset($meta->req_id)) {
69+
$data['traceID'] = $meta->req_id;
70+
} elseif (isset($meta->task_id)) {
71+
$data['traceID'] = $meta->task_id;
72+
}
73+
$data['identifier'] = $meta->identifier;
74+
}
75+
}
76+
}
4277
}

src/Middlewares/ActivityMonitor.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Closure;
1919
use Spotlibs\PhpLib\Services\Metadata;
2020
use StdClass;
21-
use Illuminate\Support\Facades\Log;
21+
use Spotlibs\PhpLib\Logs\Log;
2222
use Spotlibs\PhpLib\Services\Context;
2323

2424
/**
@@ -74,6 +74,7 @@ public function handle($request, Closure $next)
7474
$meta->request_from = $request->header('X-Request-From');
7575
$meta->user_agent = $request->header('User-Agent');
7676
$meta->version_app = $request->header('X-Version-App');
77+
$meta->identifier = $request->server('REQUEST_URI');
7778
$this->contextService->set(Metadata::class, $meta);
7879

7980
$this->contextService->set('method', $request->method());
@@ -101,20 +102,20 @@ public function terminate($request, $response)
101102
$log->clientapp = $request->header('X-App') !== null ? $request->header('X-App') : null;
102103
$log->path = $request->getPathInfo();
103104
$log->path_alias = $request->header('X-Path-Gateway') !== null ? $request->header('X-Path-Gateway') : null;
104-
$log->requestID = $request->header('X-Request-ID') !== null ? $request->header('X-Request-ID') : null;
105105
$log->requestFrom = $request->header('X-Request-From') !== null ? $request->header('X-Request-From') : null;
106106
$log->requestUser = $request->header('X-Request-User') !== null ? $request->header('X-Request-User') : null;
107107
$log->deviceID = $request->header('X-Device-ID') !== null ? $request->header('X-Device-ID') : null;
108+
$log->requestID = $request->header('X-Request-ID') !== null ? $request->header('X-Request-ID') : null;
108109
$log->requestTags = $request->header('X-Request-Tags') !== null ? $request->header('X-Request-Tags') : null;
109-
$log->requestBody = strlen(json_encode($request->all())) < 3000 ? $request->all() : null;
110-
110+
$log->requestBody = strlen(json_encode($request->all())) > 5000 ? 'more than 5000 characters' : $request->all();
111+
$this->logFileRequest($log, $request);
111112
// hashing secret information
112113
if (isset($log->requestBody['password'])) {
113114
$log->requestBody['password'] = hash('sha256', $log->requestBody['password']);
114115
}
115116
$responseObjContent = json_decode($response->getContent());
116117
if (strlen($response->getContent()) > 5000 && isset($responseObjContent->responseData)) {
117-
unset($responseObjContent->responseData);
118+
$responseObjContent->responseData = 'more than 5000 characters';
118119
}
119120
$log->responseBody = $request->getPathInfo() !== '/docs' ? $responseObjContent : ['responseCode' => '00', 'responseDesc' => 'Sukses API Docs'];
120121
$log->responseTime = round((microtime(true) - $request->server('REQUEST_TIME_FLOAT')) * 1000);
@@ -126,6 +127,36 @@ public function terminate($request, $response)
126127
)
127128
->setTimezone(new \DateTimeZone('Asia/Jakarta'))
128129
->format(\DateTimeInterface::RFC3339_EXTENDED);
129-
Log::channel('activity')->info(json_encode($log));
130+
Log::activity()->info((array) $log);
131+
}
132+
133+
/**
134+
* Write request file details to log
135+
*
136+
* @param StdClass $log pointer of log instance
137+
* @param \Illuminate\Http\Request $request pointer of http request
138+
*
139+
* @return void
140+
*/
141+
private function logFileRequest(StdClass &$log, &$request): void
142+
{
143+
foreach ($request->allFiles() as $key => $value) {
144+
$log->requestBody[$key] = [];
145+
if (is_array($files = $request->file($key))) {
146+
foreach ($files as $file) {
147+
$log->requestBody[$key][] = [
148+
'filename' => $file->getClientOriginalName(),
149+
'mimetype' => $file->getMimeType(),
150+
'size' => $file->getSize()
151+
];
152+
}
153+
continue;
154+
}
155+
$log->requestBody[$key][] = [
156+
'filename' => $value->getClientOriginalName(),
157+
'mimetype' => $value->getMimeType(),
158+
'size' => $value->getSize()
159+
];
160+
}
130161
}
131162
}

src/Providers/ContextServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Service provider for context
2525
*
2626
* @category StandardService
27-
* @package Services
27+
* @package ServiceProvider
2828
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
2929
* @license https://mit-license.org/ MIT License
3030
* @link https://github.com/spotlibs

0 commit comments

Comments
 (0)