|
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\Handler; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Logger\LogLevel; |
|
18
|
|
|
use Phossa2\Logger\Message\Message; |
|
19
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
|
20
|
|
|
use Phossa2\Logger\Exception\LogicException; |
|
21
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* StreamHandler |
|
25
|
|
|
* |
|
26
|
|
|
* Log to a stream (file, terminal, url etc.) |
|
27
|
|
|
* |
|
28
|
|
|
* @package Phossa2\Logger |
|
29
|
|
|
* @author Hong Zhang <[email protected]> |
|
30
|
|
|
* @see HandlerAbstract |
|
31
|
|
|
* @version 2.0.0 |
|
32
|
|
|
* @since 2.0.0 added |
|
33
|
|
|
*/ |
|
34
|
|
|
class StreamHandler extends HandlerAbstract |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* stream |
|
38
|
|
|
* |
|
39
|
|
|
* @var resource |
|
40
|
|
|
* @access protected |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $stream; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param string|resource $stream the stream |
|
48
|
|
|
* @param string $logLevel |
|
|
|
|
|
|
49
|
|
|
* @param FormatterInterface $formatter |
|
50
|
|
|
* @param bool $stopPropagation |
|
51
|
|
|
* @access public |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
$stream, |
|
55
|
|
|
/*# string */ $level = LogLevel::DEBUG, |
|
56
|
|
|
FormatterInterface $formatter = null, |
|
57
|
|
|
/*# bool */ $stopPropagation = false |
|
58
|
|
|
) { |
|
59
|
|
|
// open stream |
|
60
|
|
|
$strm = $this->openStream($stream); |
|
61
|
|
|
|
|
62
|
|
|
if (!is_resource($strm)) { |
|
63
|
|
|
throw new LogicException( |
|
64
|
|
|
Message::get(Message::LOG_STREAM_FAIL, $stream), |
|
65
|
|
|
Message::LOG_STREAM_FAIL |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
$this->stream = $strm; |
|
69
|
|
|
|
|
70
|
|
|
parent::__construct($level, $formatter, $stopPropagation); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function write(LogEntryInterface $logEntry) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->stream) { |
|
79
|
|
|
flock($this->stream, LOCK_EX); |
|
80
|
|
|
fwrite($this->stream, $logEntry->getFormatted() . $this->getEol()); |
|
81
|
|
|
flock($this->stream, LOCK_UN); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Open stream for writing |
|
87
|
|
|
* |
|
88
|
|
|
* @param string|resource $path |
|
89
|
|
|
* @return resource|false |
|
90
|
|
|
* @access protected |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function openStream(/*# string */ $path) |
|
93
|
|
|
{ |
|
94
|
|
|
if (is_string($path)) { |
|
95
|
|
|
if (false !== strpos($path, '://')) { |
|
96
|
|
|
$path = 'file://' . $path; |
|
97
|
|
|
} |
|
98
|
|
|
return fopen($path, 'a'); |
|
99
|
|
|
} |
|
100
|
|
|
return $path; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function close() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->stream) { |
|
109
|
|
|
fclose($this->stream); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.