-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Bee\Logger\Adapter; | ||
|
||
use Psr\Log\AbstractLogger; | ||
use Swoole\Coroutine\System; | ||
|
||
/** | ||
* Swoole 日志记录器 | ||
* | ||
* @package Bee\Logger | ||
*/ | ||
class Swoole extends AbstractLogger | ||
{ | ||
/** | ||
* 目标日志文件 | ||
* | ||
* @var string | ||
*/ | ||
protected $logFile; | ||
|
||
/** | ||
* 设置日志文件路径 | ||
* | ||
* @return void | ||
*/ | ||
public function setLogFile($filePath) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace Bee\Logger; | ||
|
||
class Exception extends \Bee\Exception | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
info $s | ||
info Test | ||
info Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Bee\Logger\Adapter\Swoole; | ||
|
||
go(function () { | ||
|
||
$logger = new Swoole; | ||
$logger->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] | ||
}); |