Skip to content

Commit

Permalink
Add log to file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
inverse committed Feb 21, 2024
1 parent e889ff1 commit b0eaa60
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Config

private int $logLevel;

private bool $logToFile;

private bool $allowMultipleNotifications;

private ?Telegram $telegram;
Expand All @@ -41,6 +43,7 @@ public function __construct(
array $sites,
array $rules,
int $logLevel,
bool $logToFile,
bool $allowMultipleNotifications,
?Pushbullet $pushbullet,
?Telegram $telegram,
Expand All @@ -49,6 +52,7 @@ public function __construct(
$this->sites = $sites;
$this->rules = $rules;
$this->logLevel = $logLevel;
$this->logToFile = $logToFile;
$this->allowMultipleNotifications = $allowMultipleNotifications;
$this->pushbullet = $pushbullet;
$this->telegram = $telegram;
Expand All @@ -73,6 +77,11 @@ public function getLogLevel(): int
return $this->logLevel;
}

public function getLogToFile(): bool
{
return $this->logToFile;
}

public function isAllowMultipleNotifications(): bool
{
return $this->allowMultipleNotifications;
Expand Down
11 changes: 10 additions & 1 deletion src/Config/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function parse(array $config): Config

$logLevel = $this->getLogLevel($config);

return new Config($sites, $rules, $logLevel, $allowMultipleNotifications, $pushbullet, $telegram, $ntfy);
$logToFile = $this->getLogToFile($config);

return new Config($sites, $rules, $logLevel, $logToFile, $allowMultipleNotifications, $pushbullet, $telegram, $ntfy);
}

private function getLogLevel(array $config): int
Expand All @@ -72,6 +74,13 @@ private function getLogLevel(array $config): int
return Logger::toMonologLevel($level);
}

private function getLogToFile(array $config): bool
{
$logger = $config['logger'] ?? [];

return $logger['file'] ?? false;
}

private function getRules(array $config): array
{
$rules = [];
Expand Down
4 changes: 3 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function __construct(Config $config)

$this[LoggerInterface::class] = static function () use ($config) {
$logger = new Logger('termin');
// $logger->pushHandler(new StreamHandler(self::ROOT_DIR.'var/log/app.log', $config->getLogLevel()));
if ($config->getLogToFile()) {
$logger->pushHandler(new StreamHandler(self::ROOT_DIR.'var/log/app.log', $config->getLogLevel()));
}
$logger->pushHandler(new StreamHandler('php://stdout', $config->getLogLevel()));

return $logger;
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ContainerTest extends TestCase
{
public function testGetTermin(): void
{
$config = new Config([], [], Logger::DEBUG, false, null, null, null);
$config = new Config([], [], Logger::DEBUG, false, false, null, null, null);
$container = new Container($config);
self::assertInstanceOf(Termin::class, $container->getTermin());
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Notifier/NotifierFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testEmpty(): void
[],
[],
Logger::INFO,
false,
true,
null,
null,
Expand All @@ -42,6 +43,7 @@ public function testPushbullet(): void
[],
[],
Logger::INFO,
false,
true,
new Pushbullet('api'),
null,
Expand All @@ -65,6 +67,7 @@ public function testTelegram(): void
[],
[],
Logger::INFO,
false,
true,
null,
new Telegram('api', '0'),
Expand All @@ -88,6 +91,7 @@ public function testNtfy(): void
[],
[],
Logger::INFO,
false,
true,
null,
null,
Expand All @@ -111,6 +115,7 @@ public function testMultiple(): void
[],
[],
Logger::INFO,
false,
true,
new Pushbullet('yolo'),
new Telegram('api', '0'),
Expand Down

0 comments on commit b0eaa60

Please sign in to comment.