From 2d0ab750851c023b38cf8836e2da690277ae7304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=93=81=E5=B2=AD=E7=8E=8B=E5=8A=9B=E5=AE=8F?= Date: Wed, 23 Oct 2019 13:09:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8D=8F=E7=A8=8B=E9=80=82?= =?UTF-8?q?=E9=85=8D=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 11 +++--- composer.lock | 76 +++++++++++++++++++++++++++--------------- src/Adapter/Swoole.php | 69 ++++++++++++++++++++++++++++++++++++++ src/Exception.php | 1 + tests/accesss.log | 3 ++ tests/swoole.php | 15 +++++++++ 6 files changed, 144 insertions(+), 31 deletions(-) create mode 100644 src/Adapter/Swoole.php create mode 100644 tests/accesss.log create mode 100644 tests/swoole.php diff --git a/composer.json b/composer.json index 6f1a3d1..5db714c 100644 --- a/composer.json +++ b/composer.json @@ -4,11 +4,10 @@ "type": "library", "require": { "php": ">=7.1", - "ext-seaslog": ">=1.9", - "bee-framework/util": "^2.1" + "bee-framework/util": "^2.1", + "psr/log": "^1.1" }, "require-dev": { - "vicens/seaslog-helper": "^0.1.0" }, "autoload": { "psr-4": { @@ -25,5 +24,9 @@ "name": "沸羊羊", "email": "anoxia@engineer.com" } - ] + ], + "suggest": { + "ext-swoole": "Coroutine file IO support", + "ext-seaslog": "The Seaslog is an effective,fast,stable log extension for PHP." + } } diff --git a/composer.lock b/composer.lock index 12adc30..a09ea1b 100644 --- a/composer.lock +++ b/composer.lock @@ -1,24 +1,30 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0a9af5d99e8f07bc63a7f8450965f98f", + "content-hash": "9ee75da68523abb32371c3b16878aa95", "packages": [ { "name": "bee-framework/util", - "version": "v2.1.0", + "version": "v2.1.3", "source": { "type": "git", "url": "https://github.com/NufangTechnology/bee-util.git", - "reference": "eecc69eb0f4b87511544dd54b79fbb2fec38c1d9" + "reference": "79332bfca20f8ac1b1147490f1ce25b13d17dce8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/NufangTechnology/bee-util/zipball/eecc69eb0f4b87511544dd54b79fbb2fec38c1d9", - "reference": "eecc69eb0f4b87511544dd54b79fbb2fec38c1d9", - "shasum": "" + "url": "https://api.github.com/repos/NufangTechnology/bee-util/zipball/79332bfca20f8ac1b1147490f1ce25b13d17dce8", + "reference": "79332bfca20f8ac1b1147490f1ce25b13d17dce8", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require-dev": { "phpunit/phpunit": "^7.2" @@ -31,54 +37,70 @@ }, "notification-url": "https://packagist.org/downloads/", "description": "Bee框架通用类库", - "time": "2019-03-04T03:40:15+00:00" - } - ], - "packages-dev": [ + "time": "2019-03-05T09:30:57+00:00" + }, { - "name": "vicens/seaslog-helper", - "version": "v0.1.0", + "name": "psr/log", + "version": "1.1.0", "source": { "type": "git", - "url": "https://github.com/vicens/SeasLog-helper.git", - "reference": "d8ac9001ba3b86843694db3c9ad919489d321666" + "url": "https://github.com/php-fig/log.git", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vicens/SeasLog-helper/zipball/d8ac9001ba3b86843694db3c9ad919489d321666", - "reference": "d8ac9001ba3b86843694db3c9ad919489d321666", - "shasum": "" + "url": "https://api.github.com/repos/php-fig/log/zipball/6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] }, "require": { - "ext-seaslog": "*", - "php": ">=5.3" + "php": ">=5.3.0" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { - "name": "vicens", - "email": "521287718@qq.com" + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" } ], - "description": "SeasLog ide helper.", + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", "keywords": [ - "seaslog" + "log", + "psr", + "psr-3" ], - "time": "2018-11-02T05:51:32+00:00" + "time": "2018-11-20T15:27:04+00:00" } ], + "packages-dev": [], "aliases": [], "minimum-stability": "stable", "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.1", - "ext-seaslog": ">=1.9" + "php": ">=7.1" }, "platform-dev": [] } diff --git a/src/Adapter/Swoole.php b/src/Adapter/Swoole.php new file mode 100644 index 0000000..fb3f10b --- /dev/null +++ b/src/Adapter/Swoole.php @@ -0,0 +1,69 @@ +logFile = $filePath; + } + + /** + * Logs with an arbitrary level. + * + * @param string $level + * @param string $message + * @param array $context + * @return bool + */ + public function log($level, $message, array $context = array()) + { + if (!$context) { + return $this->write($level, $message); + } else { + $replace = []; + foreach ($context as $key => $val) { + // 检查该值是否可以转换为字符串 + if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { + $replace['{' . $key . '}'] = $val; + } + } + + return $this->write($level, strtr($message, $replace)); + } + } + + /** + * 将日志写入文本 + * + * @return void + */ + protected function write($level, $message) + { + $message = sprintf('%s %s', $level, $message); + $message .= PHP_EOL; + + return System::writeFile($this->logFile, $message, FILE_APPEND); + } +} diff --git a/src/Exception.php b/src/Exception.php index 18c2e0c..2c8319a 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -1,4 +1,5 @@ setLogFile(__DIR__ . '/accesss.log'); + $logger->info('Test'); + + # INFO 7sd3j23h8f2h32h233f "GET /dev/stamp/getStamp/0/" 200 + # INFO time[2019-10-24 10:23:00] id[7sd3j23h8f2h32h233f] method[POST] url[/dev/stamp/getStamp/0/] code[200] total[50] resize[301] +});