Skip to content

Commit 0314521

Browse files
committed
PoC ability to clear log files + CR fixes
1 parent 182438f commit 0314521

20 files changed

+496
-163
lines changed

src/Actions/SendRequestAction.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
use WrkFlow\ApiSdkBuilder\Events\SendingRequestEvent;
2121
use WrkFlow\ApiSdkBuilder\Exceptions\ResponseException;
2222
use WrkFlow\ApiSdkBuilder\Log\Contracts\LoggerContract;
23+
use WrkFlow\ApiSdkBuilder\Log\Entities\LoggerConfigEntity;
2324
use WrkFlow\ApiSdkBuilder\Responses\AbstractResponse;
2425

25-
class SendRequestAction
26+
final class SendRequestAction
2627
{
2728
public function __construct(
2829
private readonly BuildHeadersAction $buildHeadersAction,
@@ -73,6 +74,7 @@ public function execute(
7374
api: $api,
7475
dispatcher: $dispatcher,
7576
logger: $logger,
77+
loggerConfig: $loggerConfig,
7678
request: $request,
7779
responseClass: $responseClass,
7880
requestId: $id,
@@ -87,6 +89,7 @@ public function execute(
8789
responseClass: $responseClass,
8890
dispatcher: $dispatcher,
8991
logger: $logger,
92+
loggerConfig: $loggerConfig,
9093
id: $id,
9194
request: $request,
9295
timeStart: $timeStart
@@ -96,11 +99,12 @@ public function execute(
9699
/**
97100
* @param class-string<AbstractResponse> $responseClass
98101
*/
99-
protected function sendRequest(
102+
private function sendRequest(
100103
AbstractEnvironment $environment,
101104
AbstractApi $api,
102105
?EventDispatcherInterface $dispatcher,
103106
?LoggerContract $logger,
107+
LoggerConfigEntity $loggerConfig,
104108
RequestInterface $request,
105109
string $responseClass,
106110
string $requestId,
@@ -130,13 +134,13 @@ protected function sendRequest(
130134
requestDurationInSeconds: $this->getRequestDuration($timeStart)
131135
);
132136
$dispatcher?->dispatch($event);
133-
$logger?->requestConnectionFailed($event);
137+
$logger?->requestConnectionFailed(event: $event, config: $loggerConfig);
134138

135139
throw $exception;
136140
}
137141
}
138142

139-
protected function withBody(
143+
private function withBody(
140144
AbstractApi $api,
141145
OptionsContract|StreamInterface|string|null $body,
142146
RequestInterface $request
@@ -154,7 +158,7 @@ protected function withBody(
154158
return $request;
155159
}
156160

157-
protected function getRequestDuration(float $timeStart): float
161+
private function getRequestDuration(float $timeStart): float
158162
{
159163
return (float) microtime(true) - $timeStart;
160164
}
@@ -169,13 +173,14 @@ protected function getRequestDuration(float $timeStart): float
169173
*
170174
* @return TResponse
171175
*/
172-
protected function handleResponse(
176+
private function handleResponse(
173177
AbstractApi $api,
174178
ResponseInterface $response,
175179
?int $expectedResponseStatusCode,
176180
string $responseClass,
177181
?EventDispatcherInterface $dispatcher,
178182
?LoggerContract $logger,
183+
LoggerConfigEntity $loggerConfig,
179184
string $id,
180185
RequestInterface $request,
181186
float $timeStart
@@ -199,7 +204,7 @@ protected function handleResponse(
199204
requestDurationInSeconds: $this->getRequestDuration($timeStart)
200205
);
201206
$dispatcher?->dispatch($event);
202-
$logger?->responseReceivedEvent($event);
207+
$logger?->responseReceivedEvent(event: $event, config: $loggerConfig);
203208

204209
return $finalResponse;
205210
}
@@ -218,13 +223,13 @@ protected function handleResponse(
218223
requestDurationInSeconds: $this->getRequestDuration($timeStart)
219224
);
220225
$dispatcher?->dispatch($event);
221-
$logger?->requestFailed($event);
226+
$logger?->requestFailed(event: $event, config: $loggerConfig);
222227

223228
throw $exception;
224229
}
225230
}
226231

227-
protected function buildRequest(
232+
private function buildRequest(
228233
AbstractEnvironment $environment,
229234
AbstractApi $api,
230235
array $headers,

src/Factories/ApiFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
LoggerConfigEntity $loggerConfig = null,
2828
) {
2929
// By, default no logging is used
30-
$this->loggerConfig = $loggerConfig ?? new LoggerConfigEntity('');
30+
$this->loggerConfig = $loggerConfig ?? new LoggerConfigEntity();
3131
}
3232

3333
public function response(): ResponseFactoryInterface
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WrkFlow\ApiSdkBuilder\Laravel\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use WrkFlow\ApiSdkBuilder\Log\Actions\ClearFileLogsAction;
9+
use WrkFlow\ApiSdkBuilder\Log\Entities\LoggerConfigEntity;
10+
11+
class ClearFileLogsCommand extends Command
12+
{
13+
protected $signature = 'api-sdk:logs:clear';
14+
protected $description = 'Clears request and response logs based on keep logs days config.';
15+
16+
public function handle(ClearFileLogsAction $clearFileLogsAction, LoggerConfigEntity $config): void
17+
{
18+
if ($clearFileLogsAction->execute($config)) {
19+
$this->info('Logs cleared');
20+
} else {
21+
$this->info('No logs to clear');
22+
}
23+
}
24+
}

src/Laravel/Configs/ApiSdkConfig.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
class ApiSdkConfig extends AbstractConfig
1010
{
1111
final public const KeyLogging = 'logging';
12+
final public const KeyLoggingType = 'type';
1213
final public const KeyLoggers = 'loggers';
14+
final public const KeyTimeForClearLogSchedule = 'clear_schedule_time';
15+
final public const KeyLogFileBaseDirectory = 'logs_file_base_directory';
16+
final public const KeyKeepLogFilesForDays = 'keep_log_files_for_days';
1317
final public const KeyIsTelescopeEnabled = 'is_telescope_enabled';
1418

1519
public function getLoggers(): LoggersMapEntity
@@ -19,12 +23,27 @@ public function getLoggers(): LoggersMapEntity
1923

2024
public function getLogging(): string
2125
{
22-
return $this->get(self::KeyLogging);
26+
return $this->get([self::KeyLogging, self::KeyLoggingType]);
2327
}
2428

2529
public function isTelescopeEnabled(): bool
2630
{
27-
return $this->get(self::KeyIsTelescopeEnabled);
31+
return (bool) $this->get(keyOrPath: self::KeyIsTelescopeEnabled);
32+
}
33+
34+
public function getTimeForClearSchedule(): ?string
35+
{
36+
return $this->get(keyOrPath: [self::KeyLogging, self::KeyTimeForClearLogSchedule], default: null);
37+
}
38+
39+
public function getLogFileBaseDirectory(): string
40+
{
41+
return $this->get(keyOrPath: [self::KeyLogging, self::KeyLogFileBaseDirectory]);
42+
}
43+
44+
public function getKeepLogFilesForDays(): int
45+
{
46+
return (int) $this->get(keyOrPath: [self::KeyLogging, self::KeyKeepLogFilesForDays]);
2847
}
2948

3049
protected function getConfigFileName(): string

src/Laravel/Configs/api_sdk.php

+33-22
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,43 @@
33
declare(strict_types=1);
44

55
use WrkFlow\ApiSdkBuilder\Laravel\Configs\ApiSdkConfig;
6-
use WrkFlow\ApiSdkBuilder\Log\LoggersFactory;
6+
use WrkFlow\ApiSdkBuilder\Log\Constants\LoggerConstants;
77

88
return [
9-
/**
10-
* Accepts domain and logger key separated by :
11-
* Multiple domains can be separated by comma.
12-
* - Log all: '*:info',
13-
* - Log only given domain: 'domain.com:debug',
14-
* - Log multiple domains: 'domain.com:debug,otherdomain.com:info',
15-
* - Log all except given domain: '*:info,domain.com:'
16-
*/
17-
ApiSdkConfig::KeyLogging => env('API_SDK_LOGGING', '*:info'),
18-
/**
19-
* List of available loggers key-ed by their name and their implementation.
20-
*
21-
* - You can change logger implementation by changing the implementation by overriding the list
22-
* - You can customize logger implementation by changing concrete class to corresponding interface using
23-
* container.
24-
*
25-
* @see \WrkFlow\ApiSdkBuilder\Log\LoggersFactory::defaults()
26-
* @link https://larastrict.com/TODO
27-
*/
28-
ApiSdkConfig::KeyLoggers => LoggersFactory::defaults(),
29-
309
/**
3110
* Enable passing requests events to telescopes HTTP watcher.
3211
*/
3312
ApiSdkConfig::KeyIsTelescopeEnabled => env('API_SDK_IS_TELESCOPE_ENABLED', true),
13+
14+
ApiSdkConfig::KeyLogging => [
15+
/**
16+
* Accepts domain and logger key separated by :
17+
* Multiple domains can be separated by comma.
18+
* - Log all: '*:info',
19+
* - Log only given domain: 'domain.com:debug',
20+
* - Log multiple domains: 'domain.com:debug,otherdomain.com:info',
21+
* - Log all except given domain: '*:info,domain.com:'
22+
*/
23+
ApiSdkConfig::KeyLoggingType => env('API_SDK_LOGGING', '*:info'),
24+
/**
25+
* List of available loggers key-ed by their name and their implementation.
26+
*
27+
* - You can change logger implementation by changing the implementation by overriding the list
28+
* - You can customize logger implementation by changing concrete class to corresponding interface using
29+
* container.
30+
*
31+
* @link https://larastrict.com/TODO
32+
*/
33+
ApiSdkConfig::KeyLoggers => LoggerConstants::DefaultLoggersMap,
34+
35+
/**
36+
* Base directory name where FileLogger will store log files. Stores in local app storage.
37+
*/
38+
ApiSdkConfig::KeyLogFileBaseDirectory => env('API_SDK_LOG_FILE_BASE_DIRECTORY', 'requests'),
39+
40+
/**
41+
* Base directory name where FileLogger will store log files. Stores in local app storage.
42+
*/
43+
ApiSdkConfig::KeyKeepLogFilesForDays => env('API_SDK_KEEP_LOG_FILES_FOR_DAYS', 14),
44+
],
3445
];

0 commit comments

Comments
 (0)