Skip to content

Commit 6c2f24c

Browse files
feat(logger): Modify logger instantiation and debug display error configs
1 parent 1f4eb76 commit 6c2f24c

File tree

6 files changed

+20
-44
lines changed

6 files changed

+20
-44
lines changed

src/AbstractBounce.php

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

55
namespace CrowdSecBouncer;
66

7-
use Exception;
87
use IPLib\Factory;
98
use Monolog\Formatter\LineFormatter;
109
use Monolog\Handler\RotatingFileHandler;
@@ -28,12 +27,6 @@ abstract class AbstractBounce implements IBounce
2827
/** @var array */
2928
protected $settings = [];
3029

31-
/** @var bool */
32-
protected $debug = false;
33-
34-
/** @var bool */
35-
protected $displayErrors = false;
36-
3730
/** @var LoggerInterface */
3831
protected $logger;
3932

@@ -121,44 +114,35 @@ protected function prepareBouncerConfigs(): array
121114
*
122115
* @return void
123116
* @throws CacheException
124-
* @throws InvalidArgumentException
117+
* @throws InvalidArgumentException|BouncerException
125118
*/
126119
public function run(): void
127120
{
128121
$this->bounceCurrentIp();
129122
}
130123

131-
public function setDebug(bool $debug): void
132-
{
133-
$this->debug = $debug;
134-
}
135-
136-
public function setDisplayErrors(bool $displayErrors): void
137-
{
138-
$this->displayErrors = $displayErrors;
139-
}
140-
141124
/**
142-
* @param string $logDirectoryPath
125+
* @param array $configs
143126
* @param string $loggerName
144127
* @return void
145128
*/
146-
protected function initLoggerHelper(string $logDirectoryPath, string $loggerName): void
129+
protected function initLoggerHelper(array $configs, string $loggerName): void
147130
{
148131
// Singleton for this function
149132
if ($this->logger) {
150133
return;
151134
}
152135

153136
$this->logger = new Logger($loggerName);
154-
$logPath = $logDirectoryPath . '/prod.log';
137+
$logDir = $configs['log_directory_path']??__DIR__.'/.logs';
138+
$logPath = $logDir . '/prod.log';
155139
$fileHandler = new RotatingFileHandler($logPath, 0, Logger::INFO);
156140
$fileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
157141
$this->logger->pushHandler($fileHandler);
158142

159143
// Set custom readable logger when debug=true
160-
if ($this->debug) {
161-
$debugLogPath = $logDirectoryPath . '/debug.log';
144+
if (!empty($configs['debug_mode'])) {
145+
$debugLogPath = $logDir . '/debug.log';
162146
$debugFileHandler = new RotatingFileHandler($debugLogPath, 0, Logger::DEBUG);
163147
$debugFileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
164148
$this->logger->pushHandler($debugFileHandler);

src/IBounce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer;
2222
/**
2323
* Init the logger.
2424
*/
25-
public function initLogger(): void;
25+
public function initLogger(array $configs): void;
2626

2727
/**
2828
* Get the bouncer instance.

src/RestClient/ClientAbstract.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class ClientAbstract
3232
/** @var LoggerInterface */
3333
protected $logger;
3434

35-
/** @var array */
35+
/** @var array */
3636
protected $configs;
3737

3838
/**
@@ -59,7 +59,7 @@ public function __construct(array $configs, LoggerInterface $logger)
5959
'type' => 'REST_CLIENT_INIT',
6060
'base_uri' => $this->baseUri,
6161
'timeout' => $this->timeout,
62-
'user_agent' => $this->headers['User-Agent']
62+
'user_agent' => $this->headers['User-Agent'],
6363
]);
6464
}
6565

@@ -68,10 +68,10 @@ public function __construct(array $configs, LoggerInterface $logger)
6868
*/
6969
abstract public function request(
7070
string $endpoint,
71-
array $queryParams = null,
72-
array $bodyParams = null,
71+
array $queryParams = null,
72+
array $bodyParams = null,
7373
string $method = 'GET',
74-
array $headers = null,
75-
int $timeout = null
74+
array $headers = null,
75+
int $timeout = null
7676
): ?array;
7777
}

src/RestClient/Curl.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,9 @@ public function request(
5353
/**
5454
* Retrieve Curl options.
5555
*
56-
* @param $endpoint
57-
* @param array|null $queryParams
58-
* @param array|null $bodyParams
59-
* @param string $method
60-
* @param array|null $headers
61-
* @return array
6256
*/
6357
private function createOptions(
64-
$endpoint,
58+
string $endpoint,
6559
?array $queryParams,
6660
?array $bodyParams,
6761
string $method,

src/RestClient/FileGetContents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function request(
7676
$parts = explode(' ', $http_response_header[0]);
7777
$status = 0;
7878
if (\count($parts) > 1) {
79-
$status = (int) ($parts[1]);
79+
$status = (int) $parts[1];
8080
}
8181

8282
if ($status < 200 || $status >= 300) {

src/StandaloneBounce.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public function getBouncerInstance(array $settings, bool $forceReload = false):
7575
return $this->bouncer;
7676
}
7777

78-
public function initLogger(): void
78+
public function initLogger(array $configs): void
7979
{
80-
$this->initLoggerHelper($this->getStringSettings('log_directory_path'), 'php_standalone_bouncer');
80+
$this->initLoggerHelper($configs, 'php_standalone_bouncer');
8181
}
8282

8383
/**
@@ -359,9 +359,7 @@ public function safelyBounce(array $configs): bool
359359
});
360360
try {
361361
$this->settings = $configs;
362-
$this->setDebug($this->getBoolSettings('debug_mode'));
363-
$this->setDisplayErrors($this->getBoolSettings('display_errors'));
364-
$this->initLogger();
362+
$this->initLogger($configs);
365363
if ($this->shouldBounceCurrentIp()) {
366364
$this->init($configs);
367365
$this->run();
@@ -377,7 +375,7 @@ public function safelyBounce(array $configs): bool
377375
'line' => $e->getLine(),
378376
]);
379377
}
380-
if ($this->displayErrors) {
378+
if (!empty($configs['display_errors'])) {
381379
throw $e;
382380
}
383381
}

0 commit comments

Comments
 (0)