Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#77] Improved logger. #81

Merged
merged 37 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
fdee180
Init.
tannguyen04 Mar 19, 2024
c799edb
Init.
tannguyen04 Mar 19, 2024
c04a975
Init.
tannguyen04 Mar 19, 2024
42fb668
Remove debug option.
tannguyen04 Mar 28, 2024
e4c94d5
Remove file report option.
tannguyen04 Mar 28, 2024
8ec6ae0
Update logic show message.
tannguyen04 Mar 28, 2024
3dc2875
Deployment commit
tannguyen04 Mar 28, 2024
699f40e
replace printdebug by logdebug.
tannguyen04 Mar 28, 2024
4a4998d
Delete unuse file.
tannguyen04 Mar 28, 2024
582072a
Show info using lognotice.
tannguyen04 Mar 28, 2024
dbc228c
Style output.
tannguyen04 Mar 28, 2024
3bcf172
Relace some say statements.
tannguyen04 Mar 28, 2024
a6e64ea
Test.
tannguyen04 Mar 28, 2024
cb866a4
Log notice.
tannguyen04 Mar 28, 2024
0244027
Test.
tannguyen04 Mar 28, 2024
58bf881
Update the text.
tannguyen04 Mar 28, 2024
0b59976
test.
tannguyen04 Mar 28, 2024
b9a6eb0
test.
tannguyen04 Mar 28, 2024
a8eb508
test.
tannguyen04 Mar 28, 2024
bf12210
test
tannguyen04 Mar 28, 2024
eeefe00
test
tannguyen04 Mar 28, 2024
b6db3a9
test.
tannguyen04 Mar 28, 2024
eea424e
test.
tannguyen04 Mar 28, 2024
9583e0d
test.
tannguyen04 Mar 28, 2024
9fdd617
test.
tannguyen04 Mar 28, 2024
bf1eecc
test.
tannguyen04 Mar 28, 2024
e677b62
test
tannguyen04 Mar 28, 2024
fc76966
test
tannguyen04 Mar 28, 2024
b54df34
test
tannguyen04 Mar 28, 2024
93cc512
test
tannguyen04 Mar 28, 2024
4a77553
test.
tannguyen04 Mar 29, 2024
0a2d674
test.
tannguyen04 Mar 29, 2024
60d2724
test.
tannguyen04 Mar 29, 2024
0ec8c1d
test.
tannguyen04 Mar 29, 2024
d02afd6
test.
tannguyen04 Mar 29, 2024
3acaa37
test.
tannguyen04 Mar 29, 2024
645cce2
test.
tannguyen04 Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"symfony/filesystem": "^6",
"symfony/finder": "^6",
"czproject/git-php": "^4.2",
"symfony/process": "^6"
"symfony/process": "^6",
"symfony/monolog-bridge": "^6"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
Expand Down Expand Up @@ -52,7 +53,7 @@
"lint": [
"phpcs",
"phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml",
"phpstan",
"phpstan --memory-limit=-1",
"rector --clear-cache --dry-run"
],
"lint-fix": [
Expand Down
22 changes: 7 additions & 15 deletions src/Commands/ArtifactCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DrevOps\GitArtifact\FilesystemTrait;
use DrevOps\GitArtifact\GitArtifactGit;
use DrevOps\GitArtifact\GitArtifactGitRepository;
use DrevOps\GitArtifact\LogTrait;
use DrevOps\GitArtifact\TokenTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -28,6 +29,7 @@ class ArtifactCommand extends Command {

use TokenTrait;
use FilesystemTrait;
use LogTrait;

const GIT_REMOTE_NAME = 'dst';

Expand Down Expand Up @@ -221,7 +223,11 @@ protected function configure(): void {
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($input->getOption('debug')) {
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}
$this->output = $output;
$this->logger = self::createLogger((string) $this->getName(), $output);
try {
$this->checkRequirements();
$remote = $input->getArgument('remote');
Expand All @@ -230,7 +236,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
catch (\Exception $exception) {
$output->writeln('<error>' . $exception->getMessage() . '</error>');

return Command::FAILURE;
}

Expand Down Expand Up @@ -955,16 +960,6 @@ protected function getTokenTimestamp(string $format = 'Y-m-d_H-i-s'): string {
return date($format, $this->now);
}

/**
* Check if running in debug mode.
*
* @return bool
* Check is debugging mode or not.
*/
protected function isDebug(): bool {
return $this->debug || $this->output->isDebug();
}

/**
* Write line as yell style.
*
Expand Down Expand Up @@ -1012,12 +1007,9 @@ protected function sayOkay(string $text): void {
* The args.
*/
protected function printDebug(mixed ...$args): void {
if (!$this->isDebug()) {
return;
}
$message = array_shift($args);
/* @phpstan-ignore-next-line */
$this->writeln(vsprintf($message, $args));
$this->logDebug(vsprintf($message, $args));
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/LogTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace DrevOps\GitArtifact;

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

/**
* Log trait.
*/
trait LogTrait {

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

/**
* Create Logger.
*
* @param string $name
* Name.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* Output.
*
* @return \Psr\Log\LoggerInterface
* Logger.
*/
public static function createLogger(string $name, OutputInterface $output): LoggerInterface {
$logger = new Logger($name);
$verbosityMapping = [
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,
];
$verbosity = $output->getVerbosity();
$level = $verbosityMapping[$verbosity] ?? Level::Debug;
$handler = new StreamHandler(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hello-log.log', $level);
$logger->pushHandler($handler);
$handler = new ConsoleHandler($output);
$logger->pushHandler($handler);

return $logger;
}

/**
* Log debug.
*
* @param string|\Stringable $message
* Message.
* @param array<mixed> $context
* Context.
*/
public function logDebug(string|\Stringable $message, array $context = []): void {
$this->logger->debug($message, $context);
}

}