Text   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 74
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A context() 0 15 2
A render() 0 9 1
1
<?php
2
/**
3
 * Text lines
4
 * User: moyo
5
 * Date: 10/10/2017
6
 * Time: 3:00 PM
7
 */
8
9
namespace Carno\Log\Formatter;
10
11
use Carno\Log\Contracts\Formatter;
12
use Psr\Log\LogLevel;
13
14
class Text implements Formatter
15
{
16
    /**
17
     * @var array
18
     */
19
    private $colors = [
20
        LogLevel::EMERGENCY => '0;31m', // red
21
        LogLevel::ALERT => '0;31m', // red
22
        LogLevel::CRITICAL => '0;31m', // red
23
        LogLevel::ERROR => '0;31m', // red
24
        LogLevel::WARNING => '1;33m', // yellow
25
        LogLevel::NOTICE => '0;35m', // purple
26
        LogLevel::INFO => '0;36m', // cyan
27
        LogLevel::DEBUG => '0;32m', // green
28
    ];
29
30
    /**
31
     * @var string
32
     */
33
    private $colorCtxKey = '0;33m'; // brown
34
35
    /**
36
     * @var string
37
     */
38
    private $colorMessage = '1;37m'; // white
39
40
    /**
41
     * @var string
42
     */
43
    private $colorSignEND = "\033[0m";
44
45
    /**
46
     * @var string
47
     */
48
    private $colorSignBGN = "\033[";
49
50
    /**
51
     * @param string $scene
52
     * @param string $level
53
     * @param string $message
54
     * @param array $context
55
     * @return string
56
     */
57
    public function render(string $scene, string $level, string $message, array $context) : string
58
    {
59
        return sprintf(
60
            "[%s] [%s] [%s] : %s ~ %s\n",
61
            $this->colorSignBGN . $this->colors[$level] . strtoupper($level) . $this->colorSignEND,
62
            date('Y-m-d H:i:s'),
63
            strtoupper($scene),
64
            $this->colorSignBGN . $this->colorMessage . $message . $this->colorSignEND,
65
            $this->context($context)
66
        );
67
    }
68
69
    /**
70
     * @param $context
71
     * @return string
72
     */
73
    private function context($context)
74
    {
75
        $print = '[';
76
77
        array_walk($context, function ($item, $key) use (&$print) {
78
            $ctx = $this->colorSignBGN . $this->colorCtxKey . $key . $this->colorSignEND . '=';
79
            if (is_array($item)) {
80
                $ctx .= json_encode($item, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
81
            } else {
82
                $ctx .= $item;
83
            }
84
            $print .= $ctx . ',';
85
        });
86
87
        return rtrim($print, ',') . ']';
88
    }
89
}
90