Skip to content

Commit

Permalink
log即为写日志,并选择性输出至控制台,console可选执行性是否输出至控制台
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Mar 7, 2021
1 parent 9fa0410 commit 97692df
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ private function registerErrorHandler()
$ignoreCategory = Config::getInstance()->getConf('LOG.ignoreCategory');
Logger::getInstance()->ignoreCategory($ignoreCategory);

$displayConsole = Config::getInstance()->getConf('LOG.displayConsole');
Logger::getInstance()->displayConsole($displayConsole);

//初始化追追踪器
$trigger = Di::getInstance()->get(SysConst::TRIGGER_HANDLER);
if (!$trigger instanceof TriggerInterface) {
Expand Down
49 changes: 29 additions & 20 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Logger

private $logConsole = true;

private $displayConsole = true;

private $ignoreCategory = [];

private $logLevel = LoggerInterface::LOG_LEVEL_INFO;
Expand All @@ -33,6 +35,11 @@ function __construct(LoggerInterface $logger)
$this->callback = new Event();
}

public function onLog(): Event
{
return $this->callback;
}

public function logLevel(?int $level = null)
{
if ($level !== null) {
Expand All @@ -42,6 +49,16 @@ public function logLevel(?int $level = null)
return $this->logLevel;
}

public function displayConsole(?bool $is = null)
{
if($is === null){
return $this->displayConsole;
}else{
$this->displayConsole = $is;
return $this;
}
}

public function logConsole(?bool $is = null)
{
if ($is === null) {
Expand Down Expand Up @@ -72,6 +89,10 @@ public function log(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEB
return;
}

if ($this->logConsole) {
$this->console($msg, $logLevel, $category);
}

$this->logger->log($msg, $logLevel, $category);
$calls = $this->callback->all();
foreach ($calls as $call) {
Expand All @@ -81,47 +102,35 @@ public function log(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEB

public function console(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEBUG, string $category = 'debug')
{
if ($logLevel < $this->logLevel) {
return;
}

if (in_array($category, $this->ignoreCategory)) {
return;
}

$this->logger->console($msg, $logLevel, $category);
if ($this->logConsole) {
$this->log($msg, $logLevel, $category);
if($this->displayConsole){
$this->logger->console($msg, $logLevel, $category);
}
}

public function debug(?string $msg, string $category = 'debug')
{
$this->console($msg, LoggerInterface::LOG_LEVEL_DEBUG, $category);
$this->log($msg, LoggerInterface::LOG_LEVEL_DEBUG, $category);
}

public function info(?string $msg, string $category = 'info')
{
$this->console($msg, LoggerInterface::LOG_LEVEL_INFO, $category);
$this->log($msg, LoggerInterface::LOG_LEVEL_INFO, $category);
}

public function notice(?string $msg, string $category = 'notice')
{
$this->console($msg, LoggerInterface::LOG_LEVEL_NOTICE, $category);
$this->log($msg, LoggerInterface::LOG_LEVEL_NOTICE, $category);
}

public function waring(?string $msg, string $category = 'waring')
{
$this->console($msg, LoggerInterface::LOG_LEVEL_WARNING, $category);
$this->log($msg, LoggerInterface::LOG_LEVEL_WARNING, $category);
}

public function error(?string $msg, string $category = 'error')
{
$this->console($msg, LoggerInterface::LOG_LEVEL_ERROR, $category);
$this->log($msg, LoggerInterface::LOG_LEVEL_ERROR, $category);
}

public function onLog(): Event
{
return $this->callback;
}

}
1 change: 1 addition & 0 deletions src/Resource/Config._php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ return [
'level' => LoggerInterface::LOG_LEVEL_DEBUG,
'handler' => null,
'logConsole' => true,
'displayConsole'=>true,
'ignoreCategory' => []
],
'TEMP_DIR' => null
Expand Down

0 comments on commit 97692df

Please sign in to comment.