From 4057581b9477b726aac830d6e902e8a63652dc9b Mon Sep 17 00:00:00 2001 From: Jens True Date: Wed, 31 May 2023 14:34:35 +0000 Subject: [PATCH] Added logging. --- .gitignore | 2 ++ Makefile | 5 ++++- backup | 4 +++- config.example.yml | 6 ++++-- output.log | 11 +++++++++++ src/App.php | 36 ++++++++++++++++++++++++++++++++++++ src/CommandBackup.php | 22 +++++++++++++--------- src/CommandShow.php | 8 ++++---- src/Ntfy/Ntfy.php | 7 ++++++- src/Rclone/Rclone.php | 16 ++++++++++++---- 10 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 output.log create mode 100644 src/App.php diff --git a/.gitignore b/.gitignore index 3862975..d8dc4c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /vendor/ +/output/ +/temp/ config.yml composer.phar \ No newline at end of file diff --git a/Makefile b/Makefile index 5caed5a..173c589 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,7 @@ analyze-all: install: php composer.phar install --no-dev install-dev: - php composer.phar install \ No newline at end of file + php composer.phar install +test: + ./vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage + \ No newline at end of file diff --git a/backup b/backup index 88e596b..cec3043 100755 --- a/backup +++ b/backup @@ -21,9 +21,11 @@ if (! $autoload) { } require $autoload; +use Symfony\Component\Console\Application; +use Composer\InstalledVersions; + $package = \Composer\InstalledVersions::getRootPackage(); -use Symfony\Component\Console\Application; $application = new Application('backup', $package['version']); diff --git a/config.example.yml b/config.example.yml index bb2fd3f..8df3bb4 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,12 +1,14 @@ notification: domain: ntfy.jcktrue.dk topic: backup +logging: + stream: output.log rclone: bwlimit: 6M backup: - title: Example - source: test/src - destination: test/dest + source: temp/source + destination: temp/destination templates: notify: | {{ config.title }} diff --git a/output.log b/output.log new file mode 100644 index 0000000..81b6fbb --- /dev/null +++ b/output.log @@ -0,0 +1,11 @@ +[2023-05-31T14:30:00.890085+00:00] app.INFO: Initialization complete [] [] +[2023-05-31T14:30:00.946882+00:00] rclone.INFO: Execute command ["'rclone' 'size' '--json' 'temp/source'"] [] +[2023-05-31T14:30:00.989664+00:00] rclone.INFO: Return code [0] [] +[2023-05-31T14:30:00.989960+00:00] rclone.INFO: Execute command ["'rclone' 'size' '--json' 'temp/destination'"] [] +[2023-05-31T14:30:01.047504+00:00] rclone.INFO: Return code [0] [] +[2023-05-31T14:30:01.047810+00:00] rclone.INFO: Execute command ["'rclone' 'copy' 'temp/source' 'temp/destination' '--bwlimit' '6M'"] [] +[2023-05-31T14:30:01.097485+00:00] rclone.INFO: Return code [0] [] +[2023-05-31T14:30:01.097797+00:00] rclone.INFO: Execute command ["'rclone' 'size' '--json' 'temp/destination'"] [] +[2023-05-31T14:30:01.142119+00:00] rclone.INFO: Return code [0] [] +[2023-05-31T14:30:01.174134+00:00] notification.DEBUG: Sending ntfy notification {"topic":"backup","title":"Example","message":"Example\nFrom temp/source to temp/destination\nBackup started: May 31, 2023 14:30\nSource size: 8.00B\nDestination before: 8.00B\nDestination after: 8.00B\nDestination change : 0.00B\nBackup completed: May 31, 2023 14:30\n"} [] +[2023-05-31T14:30:01.363282+00:00] notification.DEBUG: Result of ntfy notification ["{\"id\":\"iHfqUGi4dUsC\",\"time\":1685543401,\"expires\":1685586601,\"event\":\"message\",\"topic\":\"backup\",\"title\":\"Example\",\"message\":\"Example\\nFrom temp/source to temp/destination\\nBackup started: May 31, 2023 14:30\\nSource size: 8.00B\\nDestination before: 8.00B\\nDestination after: 8.00B\\nDestination change : 0.00B\\nBackup completed: May 31, 2023 14:30\"}\n"] [] diff --git a/src/App.php b/src/App.php new file mode 100644 index 0000000..a40f028 --- /dev/null +++ b/src/App.php @@ -0,0 +1,36 @@ +config = Yaml::parseFile($configFile); + + $logger = new Logger('app'); + $logger->pushHandler(new StreamHandler($this->getConfig()['logging']['stream'])); + $logger->info("Initialization complete"); + + $this->logger = $logger; + } + + + function getConfig() : array + { + return $this->config; + } + + function getLogger() : Logger + { + return $this->logger; + } +} \ No newline at end of file diff --git a/src/CommandBackup.php b/src/CommandBackup.php index 0393784..b72ff86 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -6,8 +6,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use Symfony\Component\Yaml\Yaml; -use Symfony\Component\Yaml\Exception\ParseException; + use Twig\Environment; use Twig\Loader\ArrayLoader; @@ -30,22 +29,27 @@ class CommandBackup extends Command $io->title('Start backup process'); $io->info('Opening: '.$input->getArgument('config')); + try { - $config = Yaml::parseFile($input->getArgument('config')); - } catch (ParseException $e) { + $app = new App($input->getArgument('config')); + } + catch (\Throwable $e) { $io->error('Unable to parse the YAML string: '. $e->getMessage()); return Command::FAILURE; } + $rclone = new Rclone\Rclone(); - $io->info("Rclone version: ". $rclone->getVersion()); - $ntfy = new Ntfy\Ntfy($config['notification']['domain']); + $rclone->setLogger($app->getLogger()->withName('rclone')); - $loader = new ArrayLoader($config['templates']); + $ntfy = new Ntfy\Ntfy($app->getConfig()['notification']['domain']); + $ntfy->setLogger( $app->getLogger()->withName('notification')); + + $loader = new ArrayLoader($app->getConfig()['templates']); $twig = new Environment($loader); $twig->addExtension(new Twig\AppExtension()); - foreach($io->progressIterate( $config['backup']) as $conf) { + foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) { try { $template = array(); $template['config'] = $conf; @@ -63,7 +67,7 @@ class CommandBackup extends Command } catch (\Throwable $e) { $message = $e->getMessage(); } - $ntfy->send($config['notification']['topic'], $conf['title'], $message); + $ntfy->send($app->getConfig()['notification']['topic'], $conf['title'], $message); } $io->success("Complete"); diff --git a/src/CommandShow.php b/src/CommandShow.php index b2941b4..6656cb6 100644 --- a/src/CommandShow.php +++ b/src/CommandShow.php @@ -25,15 +25,15 @@ class CommandShow extends Command $io = new SymfonyStyle($input, $output); $io->title('List backup entities'); - $io->note('Reading from: '.$input->getArgument('config')); try { - $config = Yaml::parseFile($input->getArgument('config')); - } catch (ParseException $e) { + $app = new App($input->getArgument('config')); + } + catch (\Throwable $e) { $io->error('Unable to parse the YAML string: '. $e->getMessage()); return Command::FAILURE; } - $io->table(['Description', 'Source', 'Destination'], $config['backup']); + $io->table(['Description', 'Source', 'Destination'], $app->getConfig()['backup']); $io->success("Done"); return Command::SUCCESS; diff --git a/src/Ntfy/Ntfy.php b/src/Ntfy/Ntfy.php index 3d046ef..924d52c 100644 --- a/src/Ntfy/Ntfy.php +++ b/src/Ntfy/Ntfy.php @@ -1,8 +1,11 @@ logger->debug("Sending ntfy notification", ["topic"=>$topic, "title"=>$title, "message"=>$message]); + $result = file_get_contents( 'https://'.$this->domain.'/'.$topic, false, stream_context_create( ['http' => [ 'method' => 'POST', @@ -22,6 +26,7 @@ class Ntfy ] ) ); + $this->logger->debug("Result of ntfy notification", [$result]); } } \ No newline at end of file diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index d00a214..b22274c 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -1,13 +1,19 @@ rclonePath = $rclonePath; + $this->setLogger(new NullLogger); try { $version = $this->exec('--version'); @@ -28,7 +35,6 @@ class Rclone catch(ProcessFailedException $e) { throw new Exception("Check installation of rclone"); - return; } $this->version = explode("\n", $version)[0]; @@ -68,7 +74,7 @@ class Rclone * @param array $options */ protected function exec(string $command, array $options = array()) : string - { + { $process = new Process( array_merge( [$this->rclonePath], @@ -77,14 +83,16 @@ class Rclone $options ) ); + $this->logger->info("Execute command", [$process->getCommandLine()]); $process->setTimeout(4*3600); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { + $this->logger->error("Failed execution"); throw new ProcessFailedException($process); } - + $this->logger->info("Return code", [$process->getExitCode()]); return $process->getOutput(); } } \ No newline at end of file