Skip to content

Commit

Permalink
初步完成日志组件
Browse files Browse the repository at this point in the history
  • Loading branch information
anoxia committed Dec 19, 2018
1 parent 2296c41 commit c9440df
Show file tree
Hide file tree
Showing 33 changed files with 341 additions and 2,484 deletions.
19 changes: 16 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
{
"name": "bee-framework/log",
"name": "bee-framework/logger",
"description": "Bee框架日志件",
"type": "library",
"require": {
"bee-framework/util": "^1.0",
"psr/log": "^1.1"
"psr/log": "^1.1",
"monolog/monolog": "^1.24",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"eaglewu/swoole-ide-helper": "dev-master"
"eaglewu/swoole-ide-helper": "dev-master",
"sneakybobito/phalcon-stubs": "^3.3"
},
"autoload": {
"psr-4": {
"Bee\\Logger\\": "./src"
}
},
"autoload-dev": {
"psr-4": {
"Bee\\Logger\\Tests\\": "./tests"
}
},
"authors": [
{
Expand Down
174 changes: 174 additions & 0 deletions src/Adapter/Stream.php
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;
}
}
Loading

0 comments on commit c9440df

Please sign in to comment.