Skip to content

Commit

Permalink
Simplified LoggerTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 9, 2024
1 parent aeb9f9e commit 133a5d6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 129 deletions.
26 changes: 13 additions & 13 deletions src/Commands/ArtifactCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +28,7 @@ class ArtifactCommand extends Command {

use TokenTrait;
use FilesystemTrait;
use LogTrait;
use LoggerTrait;

const GIT_REMOTE_NAME = 'dst';

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -376,7 +376,7 @@ protected function showInfo(): void {
$this->output->writeln($lines);

foreach ($lines as $line) {
$this->logNotice($line);
$this->logger->notice($line);
}
}

Expand All @@ -398,7 +398,7 @@ protected function showReport(bool $result): void {
$lines[] = '----------------------------------------------------------------------';

foreach ($lines as $line) {
$this->logNotice($line);
$this->logger->notice($line);
}
}

Expand Down Expand Up @@ -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');
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Git/ArtifactGitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@
class ArtifactGitRepository extends GitRepository {

use FilesystemTrait;
use LogTrait;
use LoggerTrait;

/**
* The gitignore file path.
Expand Down Expand Up @@ -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');
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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)));
}
}

Expand Down
108 changes: 0 additions & 108 deletions src/Traits/LogTrait.php

This file was deleted.

81 changes: 81 additions & 0 deletions src/Traits/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace DrevOps\GitArtifact\Traits;

use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Logger trait.
*/
trait LoggerTrait {

/**
* Logger.
*/
protected LoggerInterface $logger;

/**
* Path to the temporary log file.
*/
protected string $loggerDumpFile = '';

/**
* Initialize logger.
*
* @param string $name
* Logger name.
* @param \Symfony\Component\Console\Input\InputInterface $input
* Input interface.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* Output interface.
*/
protected function loggerInit(string $name, InputInterface $input, OutputInterface $output): void {
if ($input->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);
}
}

}

0 comments on commit 133a5d6

Please sign in to comment.