diff --git a/src/Config/Config.php b/src/Config/Config.php index 0c3c2ee..3fb9b0f 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -23,6 +23,8 @@ class Config private int $logLevel; + private bool $logToFile; + private bool $allowMultipleNotifications; private ?Telegram $telegram; @@ -41,6 +43,7 @@ public function __construct( array $sites, array $rules, int $logLevel, + bool $logToFile, bool $allowMultipleNotifications, ?Pushbullet $pushbullet, ?Telegram $telegram, @@ -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; @@ -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; diff --git a/src/Config/ConfigParser.php b/src/Config/ConfigParser.php index 1c87fd4..7865cbc 100644 --- a/src/Config/ConfigParser.php +++ b/src/Config/ConfigParser.php @@ -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 @@ -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 = []; diff --git a/src/Container.php b/src/Container.php index c6f8854..9e5f00b 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 78a5141..e995881 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -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()); } diff --git a/tests/Notifier/NotifierFactoryTest.php b/tests/Notifier/NotifierFactoryTest.php index 82c7795..de75744 100644 --- a/tests/Notifier/NotifierFactoryTest.php +++ b/tests/Notifier/NotifierFactoryTest.php @@ -24,6 +24,7 @@ public function testEmpty(): void [], [], Logger::INFO, + false, true, null, null, @@ -42,6 +43,7 @@ public function testPushbullet(): void [], [], Logger::INFO, + false, true, new Pushbullet('api'), null, @@ -65,6 +67,7 @@ public function testTelegram(): void [], [], Logger::INFO, + false, true, null, new Telegram('api', '0'), @@ -88,6 +91,7 @@ public function testNtfy(): void [], [], Logger::INFO, + false, true, null, null, @@ -111,6 +115,7 @@ public function testMultiple(): void [], [], Logger::INFO, + false, true, new Pushbullet('yolo'), new Telegram('api', '0'),