-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
341 additions
and
2,484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
namespace Bee\Logger\Adapter; | ||
|
||
use Bee\Logger\Exception; | ||
use Bee\Logger\Formatter\Json; | ||
use Phalcon\Logger\Adapter; | ||
use Phalcon\Logger\FormatterInterface; | ||
|
||
/** | ||
* 记录有效的 PHP Stream 至日志 | ||
* | ||
* <code> | ||
* use Phalcon\Logger; | ||
* use Phalcon\Logger\Adapter\Stream; | ||
* | ||
* $logger = new Stream("php://stderr"); | ||
* | ||
* $logger->log("This is a message"); | ||
* $logger->log(Logger::ERROR, "This is an error"); | ||
* $logger->error("This is another error"); | ||
* </code> | ||
*/ | ||
class Stream extends Adapter | ||
{ | ||
/** | ||
* File handler resource | ||
* | ||
* @var resource | ||
*/ | ||
protected $_stream; | ||
|
||
/** | ||
* 日志文件名 | ||
* | ||
* @var string | ||
*/ | ||
protected $_name = ''; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $_split = true; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_mode = 'ab'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_logExt = '.log'; | ||
|
||
/** | ||
* Stream constructor. | ||
* | ||
* @param string $name | ||
* @param null|array $options | ||
* @throws Exception | ||
*/ | ||
public function __construct($name = '', $options = null) | ||
{ | ||
$mode = 'ab'; | ||
|
||
if (isset($options['mode'])) { | ||
$mode = $options['mode']; | ||
} | ||
if (stripos($mode, 'r')) { | ||
throw new Exception('Stream must be opened in append or write mode'); | ||
} | ||
|
||
if (isset($options['ext'])) { | ||
$this->_logExt = $options['ext']; | ||
} | ||
|
||
if (!empty($name)) { | ||
$this->_name = $name; | ||
$this->_split = false; | ||
} | ||
|
||
$this->_mode = $mode; | ||
} | ||
|
||
/** | ||
* Returns the internal formatter | ||
* | ||
* @return FormatterInterface | ||
*/ | ||
public function getFormatter() : FormatterInterface | ||
{ | ||
if (!is_object($this->_formatter)) { | ||
$this->_formatter = new Json; | ||
} | ||
|
||
return $this->_formatter; | ||
} | ||
|
||
/** | ||
* 获取日志写入 stream | ||
* | ||
* @return bool|resource | ||
* @throws Exception | ||
*/ | ||
public function getStream() | ||
{ | ||
if ($this->_split) { | ||
$today = date('Y-m-d'); | ||
|
||
if ($this->_name != $today) { | ||
$this->close(); | ||
$this->_name = $today . $this->_logExt; | ||
$this->_stream = $this->createStream($this->_name, $this->_mode); | ||
} | ||
} else { | ||
if (empty($this->_stream)) { | ||
$this->_stream = $this->createStream($this->_name, $this->_mode); | ||
} | ||
} | ||
|
||
return $this->_stream; | ||
} | ||
|
||
/** | ||
* 创建 stream 流 | ||
* | ||
* @param string $name | ||
* @param string $model | ||
* @return bool|resource | ||
* @throws Exception | ||
*/ | ||
protected function createStream($name, $model) | ||
{ | ||
$stream = fopen($name, $model); | ||
if (!$stream) { | ||
throw new Exception("Can't open stream '" . $this->_name . "'"); | ||
} | ||
|
||
return $stream; | ||
} | ||
|
||
/** | ||
* Writes the log to the stream itself | ||
* | ||
* @param string $message | ||
* @param int $type | ||
* @param int $time | ||
* @param array $context | ||
* @throws Exception | ||
*/ | ||
public function logInternal(string $message, int $type, int $time, array $context) | ||
{ | ||
$stream = $this->getStream(); | ||
|
||
if (!is_resource($stream)) { | ||
throw new Exception('Cannot send message to the log because it is invalid'); | ||
} | ||
|
||
fwrite($stream, $this->getFormatter()->format($message, $type, $time, $context)); | ||
} | ||
|
||
/** | ||
* Closes the logger | ||
* | ||
* @return bool | ||
*/ | ||
public function close() : bool | ||
{ | ||
if (is_resource($this->_stream)) { | ||
return fclose($this->_stream); | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.