Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add coWrite(); use swoft/swoole-ide-helper #83

Merged
merged 3 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"text/template": "~2.3"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/db/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/framework/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
44 changes: 11 additions & 33 deletions src/framework/src/Log/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Monolog\Handler\AbstractProcessingHandler;
use Swoft\App;
use Swoole\Coroutine;

/**
* 日志文件输出器
Expand Down Expand Up @@ -55,50 +56,27 @@ protected function write(array $records)
{
// 参数
$this->createDir();
$isTask = App::isWorkerStatus();
$logFile = App::getAlias($this->logFile);
$messageText = implode("\n", $records) . "\n";

// 同步写
if ($isTask === false) {
return $this->syncWrite($logFile, $messageText);
}
// 异步写
$this->aysncWrite($logFile, $messageText);
}

/**
* 同步写文件
*
* @param string $logFile 日志路径
* @param string $messageText 文本信息
*/
private function syncWrite(string $logFile, string $messageText)
{
$fp = fopen($logFile, 'a');
if ($fp === false) {
throw new \InvalidArgumentException("Unable to append to log file: {$this->logFile}");
}
flock($fp, LOCK_EX);
fwrite($fp, $messageText);
flock($fp, LOCK_UN);
fclose($fp);

// 协程写
$this->coWrite($logFile, $messageText);
}

/**
* 异步写文件
* 协程写文件
*
* @param string $logFile 日志路径
* @param string $messageText 文本信息
*/
private function aysncWrite(string $logFile, string $messageText)
private function coWrite(string $logFile, string $messageText)
{
while (true) {
$result = \Swoole\Async::writeFile($logFile, $messageText, null, FILE_APPEND);
if ($result == true) {
break;
go(function () use ($logFile, $messageText) {
$res = Coroutine::writeFile($logFile, $messageText, FILE_APPEND);
if ($res === false) {
throw new \InvalidArgumentException("Unable to append to log file: {$this->logFile}");
}
}
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/http-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/http-message/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/memory/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/process/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/queue/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/redis/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
"require-dev": {
"swoft/service-governance": "^1.0",
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/service-governance/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/task/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require-dev": {
"swoft/process": "^1.0",
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/trace/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/view/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down