|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Bencagri\Artisan\Deployer; |
|
5
|
|
|
|
|
6
|
|
|
use Bencagri\Artisan\Deployer\Helper\Str; |
|
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
8
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
final class Logger |
|
12
|
|
|
{ |
|
13
|
|
|
private $isDebug; |
|
14
|
|
|
private $output; |
|
15
|
|
|
private $logFilePath; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Context $context) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->isDebug = $context->isDebug(); |
|
20
|
|
|
|
|
21
|
|
|
$this->output = $context->getOutput(); |
|
22
|
|
|
$this->output->setFormatter($this->createOutputFormatter()); |
|
23
|
|
|
|
|
24
|
|
|
$this->logFilePath = $context->getLogFilePath(); |
|
25
|
|
|
$this->initializeLogFile(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function log(string $message) : void |
|
29
|
|
|
{ |
|
30
|
|
|
$isPriorityMessage = Str::startsWith($message, '<h1>'); |
|
31
|
|
|
$isResultMessage = Str::contains($message, '<error>') || Str::contains($message, '<success>'); |
|
32
|
|
|
if ($this->isDebug || $isPriorityMessage || $isResultMessage) { |
|
33
|
|
|
$this->output->writeln($message); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->writeToLogFile($message); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function createOutputFormatter() : OutputFormatter |
|
40
|
|
|
{ |
|
41
|
|
|
return new OutputFormatter(true, [ |
|
42
|
|
|
'command' => new OutputFormatterStyle('yellow', null), |
|
43
|
|
|
'error' => new OutputFormatterStyle('red', null, ['bold', 'reverse']), |
|
44
|
|
|
'hook' => new OutputFormatterStyle('blue', null, ['bold']), |
|
45
|
|
|
'server' => new OutputFormatterStyle('magenta', null), |
|
46
|
|
|
'stream' => new OutputFormatterStyle(null, null), |
|
47
|
|
|
'success' => new OutputFormatterStyle('green', null, ['bold', 'reverse']), |
|
48
|
|
|
'ok' => new OutputFormatterStyle('green', null), |
|
49
|
|
|
'warn' => new OutputFormatterStyle('yellow', null), |
|
50
|
|
|
'h1' => new OutputFormatterStyle('blue', null, ['bold']), |
|
51
|
|
|
'h2' => new OutputFormatterStyle(null, null, []), |
|
52
|
|
|
'h3' => new OutputFormatterStyle(null, null, []), |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function initializeLogFile() : void |
|
57
|
|
|
{ |
|
58
|
|
|
(new Filesystem())->dumpFile($this->logFilePath, ''); |
|
59
|
|
|
$this->writeToLogFile(sprintf("%s\nDeployment started at %s\n%s", Str::lineSeparator('='), date('r'), Str::lineSeparator('='))); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function writeToLogFile(string $message) : void |
|
63
|
|
|
{ |
|
64
|
|
|
$loggedMessage = $this->processLogMessageForFile($message); |
|
65
|
|
|
file_put_contents($this->logFilePath, $loggedMessage.PHP_EOL, FILE_APPEND); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function processLogMessageForFile(string $message) : string |
|
69
|
|
|
{ |
|
70
|
|
|
$replacements = [ |
|
71
|
|
|
'/<command>(.*)<\/>/' => '"$1"', |
|
72
|
|
|
'/<server>(.*)<\/>/' => '$1', |
|
73
|
|
|
'/<stream>(.*)<\/>/' => '$1', |
|
74
|
|
|
'/<hook>(.*)<\/>/' => '__ $1 __', |
|
75
|
|
|
'/<ok>(.*)<\/>/' => '$1', |
|
76
|
|
|
'/<warn>(.*)<\/>/' => '$1', |
|
77
|
|
|
'/<h1>(.*)<\/>/' => "\n===> $1", |
|
78
|
|
|
'/<h2>(.*)<\/>/' => '---> $1', |
|
79
|
|
|
'/<h3>(.*)<\/>/' => '$1', |
|
80
|
|
|
'/<success>(.*)<\/>/' => sprintf("\n%s\n$1\n%s\n", Str::lineSeparator(), Str::lineSeparator()), |
|
81
|
|
|
'/<error>(.*)<\/>/' => sprintf("\n%s\n$1\n%s\n", Str::lineSeparator('*'), Str::lineSeparator('*')), |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
return preg_replace(array_keys($replacements), array_values($replacements), $message); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|