Skip to content

Commit

Permalink
新增协程适配器
Browse files Browse the repository at this point in the history
  • Loading branch information
anoxia committed Oct 23, 2019
1 parent 7dee21b commit 2d0ab75
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 31 deletions.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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."
}
}
76 changes: 49 additions & 27 deletions composer.lock

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

69 changes: 69 additions & 0 deletions src/Adapter/Swoole.php
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);
}
}
1 change: 1 addition & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Bee\Logger;

class Exception extends \Bee\Exception
Expand Down
3 changes: 3 additions & 0 deletions tests/accesss.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
info $s
info Test
info Test
15 changes: 15 additions & 0 deletions tests/swoole.php
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]
});

0 comments on commit 2d0ab75

Please sign in to comment.