Skip to content

Commit af3bf31

Browse files
committed
Refactor Logger: remove $input, enhance logging with fileLog integration
1 parent fdb0cd1 commit af3bf31

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

src/Deployer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function __construct(Application $console)
125125
: new NullHandler();
126126
};
127127
$this['logger'] = function ($c) {
128-
return new Logger($c['input'], $c['output'], $this['logHandler']);
128+
return new Logger($c['output'], $this['logHandler']);
129129
};
130130
$this['sshClient'] = function ($c) {
131131
return new SshClient($c['output'], $c['logger']);

src/Logger/Logger.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121

2222
class Logger
2323
{
24-
private InputInterface $input;
2524
private OutputInterface $output;
2625
private HandlerInterface $fileLog;
2726

2827
private ?float $startTime = null;
2928

30-
public function __construct(InputInterface $input, OutputInterface $output, HandlerInterface $log)
29+
public function __construct(OutputInterface $output, HandlerInterface $log)
3130
{
32-
$this->input = $input;
3331
$this->output = $output;
3432
$this->fileLog = $log;
3533
}
@@ -40,6 +38,7 @@ public function command(Host $host, string $type, string $command): void
4038
if ($this->output->isVerbose()) {
4139
$this->output->writeln("[$host] <fg=green;options=bold>$type</> $command");
4240
}
41+
$this->fileLog->log("[{$host->getAlias()}] $type: $command");
4342
}
4443

4544
/**
@@ -62,21 +61,13 @@ public function callback(Host $host, bool $forceOutput): callable
6261
public function printBuffer(string $type, Host $host, string $buffer): void
6362
{
6463
foreach (explode("\n", rtrim($buffer)) as $line) {
65-
$this->writeln($type, $host, $line);
66-
}
67-
}
68-
69-
public function writeln(string $type, Host $host, string $line): void
70-
{
71-
// Omit empty lines
72-
if (empty($line)) {
73-
return;
64+
if (empty($line)) {
65+
return;
66+
}
67+
$this->output->writeln("[$host] $line");
7468
}
75-
76-
$this->output->writeln("[$host] $line");
7769
}
7870

79-
8071
public function startTask(Task $task): void
8172
{
8273
$this->startTime = round(microtime(true) * 1000);
@@ -89,6 +80,7 @@ public function startTask(Task $task): void
8980
} else {
9081
$this->output->writeln("<fg=cyan;options=bold>task</> {$task->getName()}");
9182
}
83+
$this->fileLog->log("# task {$task->getName()}");
9284
}
9385

9486
public function endTask(Task $task): void
@@ -113,10 +105,7 @@ public function endTask(Task $task): void
113105
$this->output->writeln("<fg=yellow;options=bold>done</> {$task->getName()} $taskTime");
114106
}
115107

116-
if (!empty($this->input->getOption('profile'))) {
117-
$line = sprintf("%s\t%s\n", $task->getName(), $taskTime);
118-
file_put_contents($this->input->getOption('profile'), $line, FILE_APPEND);
119-
}
108+
$this->fileLog->log("# done {$task->getName()} $taskTime");
120109
}
121110

122111
public function endOnHost(Host $host): void
@@ -129,7 +118,6 @@ public function endOnHost(Host $host): void
129118
public function renderException(Throwable $exception, Host $host): void
130119
{
131120
if ($exception instanceof RunException) {
132-
133121
$message = "[$host] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n";
134122
if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) {
135123
$message .= "[$host] <fg=green;options=bold>run</> {$exception->getCommand()}\n";
@@ -148,7 +136,6 @@ public function renderException(Throwable $exception, Host $host): void
148136
}
149137
$message .= "[$host] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n";
150138
$this->output->write($message);
151-
152139
} else {
153140
$message = "";
154141
$class = get_class($exception);
@@ -178,6 +165,8 @@ public function renderException(Throwable $exception, Host $host): void
178165
$this->output->write($message);
179166
}
180167

168+
$this->fileLog->log($exception->__toString());
169+
181170
if ($exception->getPrevious()) {
182171
$this->renderException($exception->getPrevious(), $host);
183172
}

0 commit comments

Comments
 (0)