AnsiColorFormatter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 134
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setSlave() 0 4 1
A format() 0 17 2
A addColor() 0 11 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Logger
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Logger\Formatter;
16
17
use Phossa2\Logger\LogLevel;
18
use Phossa2\Logger\Entry\LogEntryInterface;
19
20
/**
21
 * AnsiColorFormatter
22
 *
23
 * Adding ANSI color base on the log level to the message after it is
24
 * formatted by a slave formatter. This formatter can be used with the
25
 * 'TerminalHandler'
26
 *
27
 * @package Phossa2\Logger
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     FormatterAbstract
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
class AnsiColorFormatter extends FormatterAbstract
34
{
35
    /**
36
     * foreground color
37
     *
38
     * @const
39
     */
40
    const FGCOLOR_BLACK          = "\033[0;30m";
41
    const FGCOLOR_RED            = "\033[0;31m";
42
    const FGCOLOR_GREEN          = "\033[0;32m";
43
    const FGCOLOR_YELLOW         = "\033[0;33m";
44
    const FGCOLOR_BLUE           = "\033[0;34m";
45
    const FGCOLOR_MAGENTA        = "\033[0;35m";
46
    const FGCOLOR_CYAN           = "\033[0;36m";
47
    const FGCOLOR_GRAY           = "\033[0;37m";
48
    const FGCOLOR_DARK_GRAY      = "\033[1;30m";
49
    const FGCOLOR_BRIGHT_RED     = "\033[1;31m";
50
    const FGCOLOR_BRIGHT_GREEN   = "\033[1;32m";
51
    const FGCOLOR_BRIGHT_YELLOW  = "\033[1;33m";
52
    const FGCOLOR_BRIGHT_BLUE    = "\033[1;34m";
53
    const FGCOLOR_BRIGHT_MAGENTA = "\033[1;35m";
54
    const FGCOLOR_BRIGHT_CYAN    = "\033[1;36m";
55
    const FGCOLOR_WHITE          = "\033[1;37m";
56
57
    /**
58
     * background color
59
     *
60
     * @const
61
     */
62
    const BGCOLOR_BLACK          = "\033[40m";
63
    const BGCOLOR_RED            = "\033[41m";
64
    const BGCOLOR_GREEN          = "\033[42m";
65
    const BGCOLOR_YELLOW         = "\033[43m";
66
    const BGCOLOR_BLUE           = "\033[44m";
67
    const BGCOLOR_MAGENTA        = "\033[45m";
68
    const BGCOLOR_CYAN           = "\033[46m";
69
    const BGCOLOR_WHITE          = "\033[47m";
70
    const DECO_BOLD              = "\033[1m";
71
    const DECO_UNDERLINE         = "\033[4m";
72
    const DECO_BLINK             = "\033[5m";
73
    const DECO_REVERSE           = "\033[7m";
74
    const DECO_CROSS             = "\033[9m";
75
    const DECO_END               = "\033[0m";
76
77
    /**
78
     * Color definitions for different log levels
79
     *
80
     * format  [ fgColor, bgColor, textDeco ]
81
     *
82
     * @var     array
83
     * @access  protected
84
     */
85
    protected $colors = array(
86
        LogLevel::DEBUG     => [self::FGCOLOR_GRAY, '', ''],
87
        LogLevel::INFO      => ['', '', ''],
88
        LogLevel::NOTICE    => [self::FGCOLOR_BRIGHT_GREEN, '', ''],
89
        LogLevel::WARNING   => [self::FGCOLOR_BRIGHT_YELLOW, '', ''],
90
        LogLevel::ERROR     => [self::FGCOLOR_BRIGHT_RED, '', ''],
91
        LogLevel::CRITICAL  => [self::FGCOLOR_BRIGHT_RED, '', self::DECO_UNDERLINE],
92
        LogLevel::ALERT     => [self::FGCOLOR_BRIGHT_RED, self::BGCOLOR_WHITE, ''],
93
        LogLevel::EMERGENCY => [self::FGCOLOR_BRIGHT_RED, self::BGCOLOR_WHITE, self::DECO_BLINK],
94
    );
95
96
    /**
97
     * Slave formatter
98
     *
99
     * @var    FormatterInterface
100
     * @access protected
101
     */
102
    protected $slave;
103
104
    /**
105
     * Constructor
106
     * @param  FormatterInterface $formatter slave formatter
107
     * @access public
108
     */
109
    public function __construct(FormatterInterface $formatter = null)
110
    {
111
        $this->setSlave($formatter);
112
    }
113
114
    /**
115
     * Set slave formatter
116
     *
117
     * @param  FormatterInterface $formatter the normal formatter
118
     * @access public
119
     * @api
120
     */
121
    public function setSlave(FormatterInterface $formatter = null)
122
    {
123
        $this->slave = $formatter;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    protected function format(
130
        LogEntryInterface $logEntry
131
    )/*# : string */ {
132
        // set default slave
133
        if (is_null($this->slave)) {
134
            $this->setSlave(new DefaultFormatter());
135
        }
136
137
        // format with slave first
138
        call_user_func($this->slave, $logEntry);
139
140
        // add colors
141
        return $this->addColor(
142
            $logEntry->getFormatted(),
143
            $this->colors[$logEntry->getLevel()]
144
        );
145
    }
146
147
    /**
148
     * add ansi color to text
149
     *
150
     * @param  string $text text to color
151
     * @param  array $definition coloring definition
152
     * @return string
153
     * @access protected
154
     */
155
    protected function addColor(
156
        /*# string */ $text,
157
        array $definition
158
    )/*# : string */ {
159
        $fgColor = $definition[0];
160
        $bgColor = $definition[1];
161
        $deColor = $definition[2];
162
        $prefix  = $fgColor . $bgColor . $deColor;
163
        $suffix  = $prefix ? self::DECO_END : '';
164
        return $prefix . $text . $suffix;
165
    }
166
}
167