1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer\Logger; |
||
9 | |||
10 | use Deployer\Component\ProcessRunner\Printer; |
||
11 | use Deployer\Host\Host; |
||
12 | use Deployer\Logger\Handler\HandlerInterface; |
||
13 | use Symfony\Component\Process\Process; |
||
14 | |||
15 | class Logger |
||
16 | { |
||
17 | /** |
||
18 | * @var HandlerInterface |
||
19 | */ |
||
20 | private $handler; |
||
21 | |||
22 | 14 | public function __construct(HandlerInterface $handler) |
|
23 | { |
||
24 | 14 | $this->handler = $handler; |
|
25 | 14 | } |
|
26 | |||
27 | 9 | public function log(string $message) |
|
28 | { |
||
29 | 9 | $this->handler->log("$message\n"); |
|
30 | 9 | } |
|
31 | |||
32 | public function callback(Host $host) |
||
33 | { |
||
34 | return function ($type, $buffer) use ($host) { |
||
35 | $this->printBuffer($type, $host, $buffer); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
36 | }; |
||
37 | } |
||
38 | |||
39 | 9 | public function printBuffer(Host $host, string $type, string $buffer) |
|
40 | { |
||
41 | 9 | foreach (explode("\n", rtrim($buffer)) as $line) { |
|
42 | 9 | $this->writeln($host, $type, $line); |
|
43 | } |
||
44 | 9 | } |
|
45 | |||
46 | 9 | public function writeln(Host $host, string $type, string $line) |
|
47 | { |
||
48 | 9 | $line = Printer::filterOutput($line); |
|
49 | |||
50 | // Omit empty lines |
||
51 | 9 | if (empty($line)) { |
|
52 | return; |
||
53 | } |
||
54 | |||
55 | 9 | if ($type === Process::ERR) { |
|
56 | $line = "[{$host->getAlias()}] err $line"; |
||
57 | } else { |
||
58 | 9 | $line = "[{$host->getAlias()}] $line"; |
|
59 | } |
||
60 | |||
61 | 9 | $this->log($line); |
|
62 | 9 | } |
|
63 | } |
||
64 |