-
Notifications
You must be signed in to change notification settings - Fork 3
/
Stream.php
74 lines (63 loc) · 1.58 KB
/
Stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* This file is part of the Apix Project.
*
* (c) Franck Cassedanne <franck at ouarz.net>
*
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
*/
namespace Apix\Log\Logger;
use Psr\Log\InvalidArgumentException;
use Apix\Log\LogEntry;
/**
* Stream log wrapper.
*
* @author Franck Cassedanne <franck at ouarz.net>
*/
class Stream extends AbstractLogger implements LoggerInterface
{
/**
* Holds the stream.
* @var resource
*/
protected $stream;
/**
* Constructor.
*
* @param resource|string $stream The stream to append to.
* @throws InvalidArgumentException If the stream cannot be created/opened.
*/
public function __construct($stream = 'php://stdout', $mode = 'a')
{
if (!is_resource($stream)) {
$stream = @fopen($stream, $mode);
}
if (!is_resource($stream)) {
throw new InvalidArgumentException(sprintf(
'The stream "%s" cannot be created or opened', $stream
));
}
$this->stream = $stream;
}
/**
* {@inheritDoc}
*/
public function write(LogEntry $log)
{
if (!is_resource($this->stream)) {
throw new \LogicException(
'The stream resource has been __destruct() too early'
);
}
return (bool) fwrite($this->stream, $log . $log->formatter->separator);
}
/**
* {@inheritDoc}
*/
public function close()
{
if (is_resource($this->stream)) {
fclose($this->stream);
}
}
}