|
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; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Logger\Message\Message; |
|
18
|
|
|
use Phossa2\Logger\Handler\StreamHandler; |
|
19
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
|
20
|
|
|
use Phossa2\Logger\Exception\LogicException; |
|
21
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
22
|
|
|
use Phossa2\Logger\Formatter\AnsiColorFormatter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* TerminalHandler |
|
26
|
|
|
* |
|
27
|
|
|
* Log to a terminal with or without ANSI color |
|
28
|
|
|
* |
|
29
|
|
|
* @package Phossa2\Logger |
|
30
|
|
|
* @author Hong Zhang <[email protected]> |
|
31
|
|
|
* @see StreamHandler |
|
32
|
|
|
* @version 2.0.0 |
|
33
|
|
|
* @since 2.0.0 added |
|
34
|
|
|
* @since 2.0.1 updated constructor |
|
35
|
|
|
*/ |
|
36
|
|
|
class TerminalHandler extends StreamHandler |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Use ANSI color or not |
|
40
|
|
|
* |
|
41
|
|
|
* @var bool |
|
42
|
|
|
* @access protected |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $color; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $stream the stream |
|
50
|
|
|
* @param bool $color use ANSI color formatter or not |
|
51
|
|
|
* @param FormatterInterface $formatter |
|
52
|
|
|
* @param bool $stopPropagation |
|
53
|
|
|
* @access public |
|
54
|
|
|
* @since 2.0.1 removed level param |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
/*# string */ $stream = 'php://stderr', |
|
58
|
|
|
/*# bool */ $color = true, |
|
59
|
|
|
FormatterInterface $formatter = null, |
|
60
|
|
|
/*# bool */ $stopPropagation = false |
|
61
|
|
|
) { |
|
62
|
|
|
if ($this->isCliMode()) { |
|
63
|
|
|
$this->color = (bool) $color; |
|
64
|
|
|
|
|
65
|
|
|
if (!in_array($stream, ['php://stderr', 'php://stdout'])) { |
|
66
|
|
|
throw new LogicException( |
|
67
|
|
|
Message::get(Message::LOG_STREAM_INVALID, $stream), |
|
68
|
|
|
Message::LOG_STREAM_INVALID |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
parent::__construct($stream, $formatter, $stopPropagation); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Override default |
|
77
|
|
|
* |
|
78
|
|
|
* {@inheritDoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function setFormatter(FormatterInterface $formatter = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->color) { |
|
83
|
|
|
$this->formatter = new AnsiColorFormatter($formatter); |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->formatter = $formatter; |
|
86
|
|
|
} |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Only if in CLI mode |
|
92
|
|
|
* |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function isHandling(LogEntryInterface $logEntry)/*# : bool */ |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->isCliMode(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|