From 62cf185939a46c4434fc06579353179ae6da7683 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 10 Dec 2024 08:21:31 +1100 Subject: [PATCH] Simplified LoggerTrait. --- src/Commands/ArtifactCommand.php | 26 +++---- src/Git/ArtifactGitRepository.php | 16 ++--- src/Traits/LogTrait.php | 108 ------------------------------ src/Traits/LoggerTrait.php | 81 ++++++++++++++++++++++ 4 files changed, 102 insertions(+), 129 deletions(-) delete mode 100644 src/Traits/LogTrait.php create mode 100644 src/Traits/LoggerTrait.php diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index a0729dc..e56415a 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -7,7 +7,7 @@ use CzProject\GitPhp\GitException; use DrevOps\GitArtifact\Git\ArtifactGitRepository; use DrevOps\GitArtifact\Traits\FilesystemTrait; -use DrevOps\GitArtifact\Traits\LogTrait; +use DrevOps\GitArtifact\Traits\LoggerTrait; use DrevOps\GitArtifact\Traits\TokenTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -28,7 +28,7 @@ class ArtifactCommand extends Command { use TokenTrait; use FilesystemTrait; - use LogTrait; + use LoggerTrait; const GIT_REMOTE_NAME = 'dst'; @@ -189,7 +189,7 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { $this->output = $output; - $this->logPrepare((string) $this->getName(), $input, $output); + $this->loggerInit((string) $this->getName(), $input, $output); $remote = $input->getArgument('remote'); if (empty($remote) || !is_string($remote)) { @@ -241,7 +241,7 @@ protected function doExecute(): void { if ($this->showChanges) { $this->output->writeln(sprintf('Added changes: %s', implode("\n", $changes))); - $this->logNotice(sprintf('Added changes: %s', implode("\n", $changes))); + $this->logger->notice(sprintf('Added changes: %s', implode("\n", $changes))); } if ($this->isDryRun) { @@ -279,7 +279,7 @@ protected function doExecute(): void { $this->showReport(is_null($error)); if ($this->needCleanup) { - $this->logNotice('Cleaning up'); + $this->logger->notice('Cleaning up'); $this->repo ->restoreLocalExclude() ->switchToBranch($this->originalBranch) @@ -289,7 +289,7 @@ protected function doExecute(): void { // Dump log to a file. if (!empty($this->logFile)) { - $this->logDump($this->logFile); + $this->loggerDump($this->logFile); } if (!is_null($error)) { @@ -348,9 +348,9 @@ protected function resolveOptions(string $url, array $options): void { throw new \Exception('Unable to load contents of ' . $gitignore); } - $this->logDebug('-----.gitignore---------'); - $this->logDebug($contents); - $this->logDebug('-----.gitignore---------'); + $this->logger->debug('-----.gitignore---------'); + $this->logger->debug($contents); + $this->logger->debug('-----.gitignore---------'); $this->gitignoreFile = $gitignore; $this->repo->setGitignoreFile($gitignore); @@ -376,7 +376,7 @@ protected function showInfo(): void { $this->output->writeln($lines); foreach ($lines as $line) { - $this->logNotice($line); + $this->logger->notice($line); } } @@ -398,7 +398,7 @@ protected function showReport(bool $result): void { $lines[] = '----------------------------------------------------------------------'; foreach ($lines as $line) { - $this->logNotice($line); + $this->logger->notice($line); } } @@ -443,13 +443,13 @@ protected function setMode(string $mode, array $options): void { */ protected function checkRequirements(): void { // @todo Refactor this into more generic implementation. - $this->logNotice('Checking requirements'); + $this->logger->notice('Checking requirements'); if (!$this->fsIsCommandAvailable('git')) { throw new \RuntimeException('Git command is not available'); } - $this->logNotice('All requirements were met'); + $this->logger->notice('All requirements were met'); } /** diff --git a/src/Git/ArtifactGitRepository.php b/src/Git/ArtifactGitRepository.php index 8707fda..1a4e535 100644 --- a/src/Git/ArtifactGitRepository.php +++ b/src/Git/ArtifactGitRepository.php @@ -8,7 +8,7 @@ use CzProject\GitPhp\IRunner; use CzProject\GitPhp\RunnerResult; use DrevOps\GitArtifact\Traits\FilesystemTrait; -use DrevOps\GitArtifact\Traits\LogTrait; +use DrevOps\GitArtifact\Traits\LoggerTrait; use Psr\Log\LoggerInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -21,7 +21,7 @@ class ArtifactGitRepository extends GitRepository { use FilesystemTrait; - use LogTrait; + use LoggerTrait; /** * The gitignore file path. @@ -161,7 +161,7 @@ public function disableLocalExclude(): static { $filename = $this->getRepositoryPath() . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR . 'info' . DIRECTORY_SEPARATOR . 'exclude'; if ($this->fs->exists($filename)) { - $this->logDebug('Disabling local exclude'); + $this->logger->debug('Disabling local exclude'); $this->fs->rename($filename, $filename . '.bak'); } @@ -175,7 +175,7 @@ public function restoreLocalExclude(): static { $filename = $this->getRepositoryPath() . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR . 'info' . DIRECTORY_SEPARATOR . 'exclude'; if ($this->fs->exists($filename . '.bak')) { - $this->logDebug('Restoring local exclude'); + $this->logger->debug('Restoring local exclude'); $this->fs->rename($filename . '.bak', $filename); } @@ -254,7 +254,7 @@ public function getOriginalBranch(): string { public function removeIgnoredFiles(): static { if (!empty($this->gitignoreFile)) { $gitignore = $this->getRepositoryPath() . DIRECTORY_SEPARATOR . '.gitignore'; - $this->logDebug(sprintf('Copying custom .gitignore file from %s to %s', $this->gitignoreFile, $gitignore)); + $this->logger->debug(sprintf('Copying custom .gitignore file from %s to %s', $this->gitignoreFile, $gitignore)); $this->fs->copy($this->gitignoreFile, $gitignore, TRUE); // Remove custom .gitignore file if it is within the repository. @@ -276,7 +276,7 @@ public function removeIgnoredFiles(): static { foreach ($files as $file) { $filename = $this->getRepositoryPath() . DIRECTORY_SEPARATOR . $file; if ($this->fs->exists($filename)) { - $this->logDebug(sprintf('Removing ignored file %s', $filename)); + $this->logger->debug(sprintf('Removing ignored file %s', $filename)); $this->fs->remove($filename); } } @@ -297,7 +297,7 @@ public function removeOtherFiles(): static { foreach ($files as $file) { $filename = $this->getRepositoryPath() . DIRECTORY_SEPARATOR . $file; if ($this->fs->exists($filename)) { - $this->logDebug(sprintf('Removing other file %s', $filename)); + $this->logger->debug(sprintf('Removing other file %s', $filename)); $this->fs->remove($filename); } } @@ -324,7 +324,7 @@ public function removeSubRepositories(): static { if ($dir instanceof \SplFileInfo) { $dir = $dir->getPathname(); $this->fs->remove($dir); - $this->logDebug(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath((string) $dir))); + $this->logger->debug(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath((string) $dir))); } } diff --git a/src/Traits/LogTrait.php b/src/Traits/LogTrait.php deleted file mode 100644 index 5755e10..0000000 --- a/src/Traits/LogTrait.php +++ /dev/null @@ -1,108 +0,0 @@ -getOption('log')) { - $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); - } - - $this->logDumpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; - - $this->logger = new Logger($name); - - $handler = new ConsoleHandler($output); - $this->logger->pushHandler($handler); - - if (!empty($this->logDumpFile)) { - $map = [ - OutputInterface::VERBOSITY_QUIET => Level::Error, - OutputInterface::VERBOSITY_NORMAL => Level::Warning, - OutputInterface::VERBOSITY_VERBOSE => Level::Notice, - OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, - OutputInterface::VERBOSITY_DEBUG => Level::Debug, - ]; - - $handler = new StreamHandler($this->logDumpFile, $map[$output->getVerbosity()] ?? Level::Debug); - - $this->logger->pushHandler($handler); - } - - $this->logDebug('Debug messages enabled'); - } - - /** - * Log debug. - * - * @param string|\Stringable $message - * Message. - * @param array $context - * Context. - */ - public function logDebug(string|\Stringable $message, array $context = []): void { - $this->logger->debug($message, $context); - } - - /** - * Log notice. - * - * @param string|\Stringable $message - * Message. - * @param array $context - * Context. - */ - public function logNotice(string|\Stringable $message, array $context = []): void { - $this->logger->notice($message, $context); - } - - /** - * Log error. - * - * @param string|\Stringable $message - * Message. - * @param array $context - * Context. - */ - public function logError(string|\Stringable $message, array $context = []): void { - $this->logger->error($message, $context); - } - - /** - * Dump log to file. - */ - protected function logDump(string $filename): void { - if ($this->fs->exists($this->logDumpFile)) { - $this->fs->copy($this->logDumpFile, $filename); - $this->fs->remove($this->logDumpFile); - } - } - -} diff --git a/src/Traits/LoggerTrait.php b/src/Traits/LoggerTrait.php new file mode 100644 index 0000000..7799f98 --- /dev/null +++ b/src/Traits/LoggerTrait.php @@ -0,0 +1,81 @@ +getOption('log')) { + $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + } + + $this->loggerDumpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; + + $this->logger = new Logger($name); + + $console_handler = new ConsoleHandler($output); + $this->logger->pushHandler($console_handler); + + if (!empty($this->loggerDumpFile)) { + $map = [ + OutputInterface::VERBOSITY_QUIET => Level::Error, + OutputInterface::VERBOSITY_NORMAL => Level::Warning, + OutputInterface::VERBOSITY_VERBOSE => Level::Notice, + OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, + OutputInterface::VERBOSITY_DEBUG => Level::Debug, + ]; + + $stream_handler = new StreamHandler($this->loggerDumpFile, $map[$output->getVerbosity()] ?? Level::Debug); + $this->logger->pushHandler($stream_handler); + } + + $this->logger->debug('Debug messages enabled'); + } + + /** + * Dump logger to file. + * + * @param string $filename + * Filename to dump the logger to. + */ + protected function loggerDump(string $filename): void { + if ($this->fs->exists($this->loggerDumpFile)) { + $this->fs->copy($this->loggerDumpFile, $filename); + $this->fs->remove($this->loggerDumpFile); + } + } + +}