-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.php
87 lines (77 loc) · 1.81 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
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* This file is part of bops
*
* @copyright Copyright (C) 2020 Jayson Wang
* @license MIT License
* @link https://github.com/wjiec/php-bops
*/
namespace Bops\Logger\Adapter;
use Bops\Exception\Framework\Logger\UnknownStreamException;
use Bops\Logger\AbstractLogger;
use Bops\Logger\Formatter\Adapter\Plain;
use Bops\Logger\Formatter\FormatterInterface;
/**
* Class File
*
* @package Bops\Logger\Adapter
*/
class Stream extends AbstractLogger {
/**
* Log filename
*
* @var string
*/
protected $filename;
/**
* File handler
*
* @var resource
*/
protected $handler;
/**
* File constructor.
*
* @param string $filename
* @throws UnknownStreamException
*/
public function __construct(string $filename) {
$this->filename = $filename;
$this->handler = fopen($filename, 'ab');
if (!is_resource($this->handler)) {
throw new UnknownStreamException("Can't open log file at {$filename}");
}
}
/**
* Closes the logger
*
* @return bool
*/
public function close(): bool {
if (is_resource($this->handler)) {
return fclose($this->handler);
}
return true;
}
/**
* @inheritDoc
*
* @return FormatterInterface
*/
protected function getDefaultFormatter(): FormatterInterface {
return new Plain();
}
/**
* @inheritDoc
*
* @param string $level
* @param string $message
* @param array $context
* @return mixed|void
*/
protected function doLog(string $level, string $message, array $context = []) {
if (is_resource($this->handler)) {
fwrite($this->handler, $this->getFormatter()->format($level, $message, $context));
}
}
}