1 | <?php |
||
2 | |||
3 | |||
4 | namespace LogicalSteps\Async; |
||
5 | |||
6 | |||
7 | use Psr\Log\AbstractLogger; |
||
8 | |||
9 | class ConsoleLogger extends AbstractLogger |
||
10 | { |
||
11 | private $startTime; |
||
12 | |||
13 | private static $colors = [ |
||
14 | 'black' => 30, |
||
15 | 'red' => 31, |
||
16 | 'green' => 32, |
||
17 | 'yellow' => 33, |
||
18 | 'blue' => 34, |
||
19 | 'magenta' => 35, |
||
20 | 'cyan' => 36, |
||
21 | 'brightGrey' => 37, |
||
22 | 'grey' => 90, |
||
23 | 'brightRed' => 91, |
||
24 | 'brightGreen' => 92, |
||
25 | 'brightYellow' => 93, |
||
26 | 'brightBlue' => 94, |
||
27 | 'brightMagenta' => 95, |
||
28 | 'brightCyan' => 96, |
||
29 | 'white' => 97 |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @var callable |
||
34 | */ |
||
35 | private $echo; |
||
36 | |||
37 | public function __construct(callable $loggingFunction = null) |
||
38 | { |
||
39 | $this->echo = $loggingFunction ?? function ($message) { |
||
40 | echo $message; |
||
41 | }; |
||
42 | $this->startTime = microtime(true); |
||
43 | } |
||
44 | |||
45 | public $consoleColors = true; |
||
46 | |||
47 | /** |
||
48 | * Logs with an arbitrary level. |
||
49 | * |
||
50 | * @param mixed $level |
||
51 | * @param string $message |
||
52 | * @param array $context |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function log($level, $message, array $context = array()) |
||
57 | { |
||
58 | switch ($message) { |
||
59 | case 'start': |
||
60 | $message = ' ┌▀' . $this->backgroundColor(' start ', 'green'); |
||
61 | break; |
||
62 | case 'end': |
||
63 | $message = ' └▄' . $this->backgroundColor(' stop ', 'brightRed'); |
||
64 | break; |
||
65 | default: |
||
66 | if ($this->consoleColors) { |
||
67 | foreach (Async::ACTIONS as $action) { |
||
68 | $message = str_replace("$action ", $this->color("$action ", 'brightGrey'), $message); |
||
69 | } |
||
70 | } |
||
71 | $depth = $context['depth'] ?? 0; |
||
72 | $depth = $depth |
||
73 | ? ' │' . str_repeat(" ", $depth * 2) . $this->color('¤ ', 'yellow') |
||
74 | : ' ├' . $this->color('» ', 'brightYellow'); |
||
75 | $message = $depth . $message; |
||
76 | } |
||
77 | $elapsed = round((microtime(true) - $this->startTime)); |
||
78 | ($this->echo)(' ' . str_pad((string)$elapsed, 5, '0', STR_PAD_LEFT) |
||
79 | . ' ' . $this->backgroundColor("[$level]", 'cyan') . $message . PHP_EOL); |
||
80 | } |
||
81 | |||
82 | private function color(string $text, string $color) |
||
83 | { |
||
84 | if (!$this->consoleColors) { |
||
85 | return $text; |
||
86 | } |
||
87 | $code = static::$colors[$color]; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
88 | return "\033[0;{$code}m{$text}\033[0m"; |
||
89 | } |
||
90 | |||
91 | private function backgroundColor(string $text, string $color) |
||
92 | { |
||
93 | if (!$this->consoleColors) { |
||
94 | return $text; |
||
95 | } |
||
96 | $code = static::$colors[$color] + 10; |
||
0 ignored issues
–
show
|
|||
97 | return "\033[0;30m\033[{$code}m{$text}\033[0m"; |
||
98 | } |
||
99 | |||
100 | } |
||
101 |