Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(Psr): Use Psr\Log to simple Rid\Log Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Feb 21, 2019
1 parent 6dc635a commit 342300f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 116 deletions.
16 changes: 5 additions & 11 deletions apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,11 @@
],

// 日志
'log' => [
// 类路径
'class' => Rid\Log\Log::class,
// 日志记录级别
'level' => ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'],
// 日志目录
'dir' => 'logs',
// 日志轮转类型
'rotate' => Rid\Log\Log::ROTATE_DAY,
// 最大文件尺寸
'maxFileSize' => 0,
'log' => [
'class' => Rid\Log\Log::class, // 类路径
'dir' => 'logs', // 日志目录
'rotate' => Rid\Log\Log::ROTATE_DAY, // 日志轮转类型
'maxFileSize' => 0, // 最大文件尺寸
],

// Token
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ext-swoole": "*",
"ext-mbstring": "*",
"ext-simplexml": "*",
"psr/log": "^1.1",
"robthree/twofactorauth": "^1.6",
"swiftmailer/swiftmailer": "^6.1",
"twig/twig": "^2.0",
Expand Down
93 changes: 45 additions & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 17 additions & 57 deletions framework/Log/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Rid\Log;

use Psr\Log\LogLevel;
use Psr\Log\LoggerTrait;
use Psr\Log\LoggerInterface;

use Rid\Base\Component;

/**
* Log组件
*/
class Log extends Component
class Log extends Component implements LoggerInterface
{
use LoggerTrait;

// 轮转规则
const ROTATE_HOUR = 0;
Expand All @@ -19,7 +24,10 @@ class Log extends Component
public $dir = 'logs';

// 日志记录级别
public $level = ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'];
public $level = [
LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR,
LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO, LogLevel::DEBUG
];

// 日志轮转类型
public $rotate = self::ROTATE_DAY;
Expand All @@ -35,67 +43,19 @@ public function onInitialize()
$this->setCoroutineMode(Component::COROUTINE_MODE_REFERENCE);
}

// emergency日志
public function emergency($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// alert日志
public function alert($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// critical日志
public function critical($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// error日志
public function error($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// warning日志
public function warning($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// notice日志
public function notice($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// info日志
public function info($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// debug日志
public function debug($message, array $context = [])
{
return $this->log(__FUNCTION__, $message, $context);
}

// 记录日志
public function log($level, $message, array $context = [])
{
if (in_array($level, $this->level)) {
return $this->write($level, $message, $context);
}
return false;
throw new \Psr\Log\InvalidArgumentException("Log level $level is not allowed.");
}

// 写入日志
public function write($filePrefix, $message, array $context = [])
{
$file = $this->getFile($filePrefix);
$file = $this->getFile($filePrefix);
$message = $this->getMessage($message, $context);
return error_log($message . PHP_EOL, 3, $file);
}
Expand All @@ -110,21 +70,21 @@ protected function getFile($filePrefix)
}
switch ($this->rotate) {
case self::ROTATE_HOUR:
$subDir = date('Ymd');
$subDir = date('Ymd');
$timeFormat = date('YmdH');
break;
case self::ROTATE_DAY:
$subDir = date('Ym');
$subDir = date('Ym');
$timeFormat = date('Ymd');
break;
case self::ROTATE_WEEKLY:
default:
$subDir = date('Y');
$subDir = date('Y');
$timeFormat = date('YW');
break;
}
$filename = "{$logDir}/{$subDir}/{$filePrefix}_{$timeFormat}";
$file = "{$filename}.log";
$file = "{$filename}.log";
// 创建目录
$dir = dirname($file);
is_dir($dir) or mkdir($dir, 0777, true);
Expand All @@ -147,7 +107,7 @@ protected function getMessage($message, array $context = [])
}
$message = strtr($message, $replace);
// 增加时间
$time = date('Y-m-d H:i:s');
$time = date('Y-m-d H:i:s');
$message = "[time] {$time} [message] {$message}";
return $message;
}
Expand Down

0 comments on commit 342300f

Please sign in to comment.