|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Monolog package. |
|
|
5 |
* |
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Monolog\Handler; |
|
|
13 |
|
|
|
14 |
use Monolog\Formatter\SimpleFormatter; |
|
|
15 |
use Monolog\Logger; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Stores to any stream resource |
|
|
19 |
* |
|
|
20 |
* Can be used to store into php://stderr, remote and local files, etc. |
|
|
21 |
* |
|
|
22 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
|
|
23 |
*/ |
|
|
24 |
class StreamHandler extends AbstractProcessingHandler |
|
|
25 |
{ |
|
|
26 |
protected $stream; |
|
|
27 |
protected $url; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* @param string $stream |
|
|
31 |
* @param integer $level The minimum logging level at which this handler will be triggered |
|
|
32 |
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
|
33 |
*/ |
|
|
34 |
public function __construct($stream, $level = Logger::DEBUG, $bubble = true) |
|
|
35 |
{ |
|
|
36 |
parent::__construct($level, $bubble); |
|
|
37 |
if (is_resource($stream)) { |
|
|
38 |
$this->stream = $stream; |
|
|
39 |
} else { |
|
|
40 |
$this->url = $stream; |
|
|
41 |
} |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* {@inheritdoc} |
|
|
46 |
*/ |
|
|
47 |
public function close() |
|
|
48 |
{ |
|
|
49 |
if (is_resource($this->stream)) { |
|
|
50 |
fclose($this->stream); |
|
|
51 |
} |
|
|
52 |
$this->stream = null; |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* {@inheritdoc} |
|
|
57 |
*/ |
|
|
58 |
protected function write(array $record) |
|
|
59 |
{ |
|
|
60 |
if (null === $this->stream) { |
|
|
61 |
if (!$this->url) { |
|
|
62 |
throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); |
|
|
63 |
} |
|
|
64 |
$this->stream = @fopen($this->url, 'a'); |
|
|
65 |
if (!is_resource($this->stream)) { |
|
|
66 |
$this->stream = null; |
|
|
67 |
throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened; it may be invalid or not writable.', $this->url)); |
|
|
68 |
} |
|
|
69 |
} |
|
|
70 |
fwrite($this->stream, (string) $record['formatted']); |
|
|
71 |
} |
|
|
72 |
} |