diff --git a/apps/config/http_base.php b/apps/config/http_base.php index 4a84911..b001359 100644 --- a/apps/config/http_base.php +++ b/apps/config/http_base.php @@ -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 diff --git a/composer.json b/composer.json index 43ba1b5..cad4746 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 8c8ec66..e48e812 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "e5f9855298bfe6db4ad5ebdf1a3e7b9f", + "content-hash": "c480423054d50d7c7ff18890f922cc75", "packages": [ { "name": "doctrine/lexer", @@ -247,17 +247,17 @@ "time": "2018-12-26T22:52:19+00:00" }, { - "name": "robthree/twofactorauth", - "version": "1.6.5", + "name": "psr/log", + "version": "1.1.0", "source": { "type": "git", - "url": "https://github.com/RobThree/TwoFactorAuth.git", - "reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2" + "url": "https://github.com/php-fig/log.git", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/f5f58a4c62d0336a0e6175856894a51f3565dad2", - "reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2", + "url": "https://api.github.com/repos/php-fig/log/zipball/6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", "shasum": "", "mirrors": [ { @@ -269,13 +269,15 @@ "require": { "php": ">=5.3.0" }, - "require-dev": { - "phpunit/phpunit": "@stable" - }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, "autoload": { "psr-4": { - "RobThree\\Auth\\": "lib" + "Psr\\Log\\": "Psr/Log/" } }, "notification-url": "https://packagist.org/downloads/", @@ -284,37 +286,31 @@ ], "authors": [ { - "name": "Rob Janssen", - "homepage": "http://robiii.me", - "role": "Developer" + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" } ], - "description": "Two Factor Authentication", - "homepage": "https://github.com/RobThree/TwoFactorAuth", + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", "keywords": [ - "Authentication", - "MFA", - "Multi Factor Authentication", - "Two Factor Authentication", - "authenticator", - "authy", - "php", - "tfa" + "log", + "psr", + "psr-3" ], - "time": "2018-06-09T10:09:59+00:00" + "time": "2018-11-20T15:27:04+00:00" }, { - "name": "sandfoxme/bencode", - "version": "1.2.0", + "name": "robthree/twofactorauth", + "version": "1.6.5", "source": { "type": "git", - "url": "https://github.com/sandfoxme/bencode.git", - "reference": "e827c34253024d84917f0f35935b2e1916311ecd" + "url": "https://github.com/RobThree/TwoFactorAuth.git", + "reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sandfoxme/bencode/zipball/e827c34253024d84917f0f35935b2e1916311ecd", - "reference": "e827c34253024d84917f0f35935b2e1916311ecd", + "url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/f5f58a4c62d0336a0e6175856894a51f3565dad2", + "reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2", "shasum": "", "mirrors": [ { @@ -324,20 +320,15 @@ ] }, "require": { - "php": ">= 7.0" + "php": ">=5.3.0" }, "require-dev": { - "codeclimate/php-test-reporter": "*", - "phpunit/phpunit": "^6.0.0", - "psy/psysh": "^0.8.3" - }, - "suggest": { - "php-64bit": "Running 64 bit is recommended to prevent integer overflow" + "phpunit/phpunit": "@stable" }, "type": "library", "autoload": { "psr-4": { - "SandFoxMe\\Bencode\\": "src" + "RobThree\\Auth\\": "lib" } }, "notification-url": "https://packagist.org/downloads/", @@ -346,18 +337,24 @@ ], "authors": [ { - "name": "Anton Smirnov", - "email": "sandfox@sandfox.me" + "name": "Rob Janssen", + "homepage": "http://robiii.me", + "role": "Developer" } ], - "description": "BitTorrent's Bencode encoder/decoder", + "description": "Two Factor Authentication", + "homepage": "https://github.com/RobThree/TwoFactorAuth", "keywords": [ - "bencode", - "bittorrent", - "serialize", - "torrent" + "Authentication", + "MFA", + "Multi Factor Authentication", + "Two Factor Authentication", + "authenticator", + "authy", + "php", + "tfa" ], - "time": "2018-02-14T19:16:21+00:00" + "time": "2018-06-09T10:09:59+00:00" }, { "name": "swiftmailer/swiftmailer", @@ -1050,7 +1047,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.2.0", + "php": ">=7.3.0", "ext-gd": "*", "ext-pdo": "*", "ext-json": "*", diff --git a/framework/Log/Log.php b/framework/Log/Log.php index e34b1b4..a3187d5 100644 --- a/framework/Log/Log.php +++ b/framework/Log/Log.php @@ -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; @@ -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; @@ -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); } @@ -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); @@ -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; }