Skip to content

Commit 34ac6c0

Browse files
committed
async log
1 parent 64c0635 commit 34ac6c0

File tree

4 files changed

+84
-17
lines changed

4 files changed

+84
-17
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ composer require "kucoin/kucoin-php-sdk:~1.1.0"
3838
KuCoinApi::setBaseUri('https://openapi-sandbox.kucoin.com');
3939
```
4040

41+
### Debug mode & logging
42+
43+
```php
44+
// Debug mode will record the logs of API and WebSocket to files in the directory "KuCoinApi::getDefaultLogPath()" according to the minimum log level "KuCoinApi::getLogLevel()".
45+
KuCoinApi::setDebugMode(true);
46+
47+
// Logging in your code
48+
// KuCoinApi::setLogPath('/tmp');
49+
// KuCoinApi::setLogLevel(Monolog\Logger::DEBUG);
50+
KuCoinApi::getLogger()->debug('I\'am a debug message');
51+
```
52+
4153
### Examples
4254
> See the [test case](tests) for more examples.
4355
@@ -251,6 +263,12 @@ go(function () {
251263
> Modify your API key in `phpunit.xml` first.
252264
253265
```shell
266+
# Add your API configuration items into the environmental variable first
267+
export API_BASE_URI=https://openapi-v2.kucoin.com
268+
export API_KEY=key
269+
export API_SECRET=secret
270+
export API_PASSPHRASE=passphrase
271+
254272
composer test
255273
```
256274

src/Api.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ abstract class Api
2828
*/
2929
protected static $debugMode = false;
3030

31+
/**
32+
* @var string
33+
*/
34+
protected static $logPath = '/tmp';
35+
36+
/**
37+
* @var LoggerInterface $logger
38+
*/
39+
protected static $logger;
40+
41+
/**
42+
* @var int
43+
*/
44+
protected static $logLevel = Logger::DEBUG;
45+
3146
/**
3247
* @var IAuth $auth
3348
*/
@@ -38,11 +53,6 @@ abstract class Api
3853
*/
3954
protected $http;
4055

41-
/**
42-
* @var LoggerInterface $logger
43-
*/
44-
protected $logger;
45-
4656
public function __construct(IAuth $auth = null, IHttp $http = null)
4757
{
4858
if ($http === null) {
@@ -103,25 +113,57 @@ public static function setDebugMode($debugMode)
103113
/**
104114
* @param LoggerInterface $logger
105115
*/
106-
public function setLogger(LoggerInterface $logger)
116+
public static function setLogger(LoggerInterface $logger)
107117
{
108-
$this->logger = $logger;
118+
self::$logger = $logger;
109119
}
110120

111121
/**
112122
* @return Logger|LoggerInterface
113123
* @throws \Exception
114124
*/
115-
public function getLogger()
125+
public static function getLogger()
116126
{
117-
if ($this->logger === null) {
118-
$this->logger = new Logger('kucoin-sdk');
119-
$handler = new RotatingFileHandler('/tmp/kucoin-sdk.log', 0, Logger::DEBUG);
127+
if (self::$logger === null) {
128+
self::$logger = new Logger('kucoin-sdk');
129+
$handler = new RotatingFileHandler(static::getLogPath() . '/kucoin-sdk.log', 0, static::$logLevel);
120130
$formatter = new LineFormatter(null, null, false, true);
121131
$handler->setFormatter($formatter);
122-
$this->logger->pushHandler($handler);
132+
self::$logger->pushHandler($handler);
123133
}
124-
return $this->logger;
134+
return self::$logger;
135+
}
136+
137+
/**
138+
* @return string
139+
*/
140+
public static function getLogPath()
141+
{
142+
return self::$logPath;
143+
}
144+
145+
/**
146+
* @param string $logPath
147+
*/
148+
public static function setLogPath($logPath)
149+
{
150+
self::$logPath = $logPath;
151+
}
152+
153+
/**
154+
* @return int
155+
*/
156+
public static function getLogLevel()
157+
{
158+
return self::$logLevel;
159+
}
160+
161+
/**
162+
* @param int $logLevel
163+
*/
164+
public static function setLogLevel($logLevel)
165+
{
166+
self::$logLevel = $logLevel;
125167
}
126168

127169
/**
@@ -155,11 +197,11 @@ public function call($method, $uri, array $params = [], array $headers = [], $ti
155197
$requestId = uniqid();
156198

157199
if (self::isDebugMode()) {
158-
$this->getLogger()->debug(sprintf('Sent a HTTP request#%s: %s', $requestId, $request));
200+
static::getLogger()->debug(sprintf('Sent a HTTP request#%s: %s', $requestId, $request));
159201
}
160202
$response = $this->http->request($request, $timeout);
161203
if (self::isDebugMode()) {
162-
$this->getLogger()->debug(sprintf('Received a HTTP response#%s: %s', $requestId, $response));
204+
static::getLogger()->debug(sprintf('Received a HTTP response#%s: %s', $requestId, $response));
163205
}
164206

165207
return $response;

src/Http/SwooleHttp.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
use Swlib\Http\ContentType;
88
use Swlib\Http\Exception\RequestException;
99
use Swlib\Saber;
10+
use Swoole\Runtime;
1011

1112
class SwooleHttp extends BaseHttp
1213
{
1314
protected static $clients = [];
1415

16+
public function __construct(array $config = [])
17+
{
18+
parent::__construct($config);
19+
Runtime::enableCoroutine();
20+
}
21+
1522
/**
1623
* @param array $config
1724
* @return Saber

src/PrivateApi/WebSocketFeed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function subscribeChannels(array $server, array $channels, callable $onMe
117117
$ping = $this->createPingMessage();
118118
$pingStr = json_encode($ping);
119119
if (self::isDebugMode()) {
120-
$this->getLogger()->debug(sprintf('Sent a WebSocket message: %s', $pingStr));
120+
static::getLogger()->debug(sprintf('Sent a WebSocket message: %s', $pingStr));
121121
}
122122
// fputs(STDIN, print_r($ping, true));
123123
$ws->send($pingStr);
@@ -128,7 +128,7 @@ public function subscribeChannels(array $server, array $channels, callable $onMe
128128
$ws->on('message', function (MessageInterface $msg) use ($server, $ws, $channels, $onMessage, $loop, $pingTimer) {
129129
$msgStr = $msg->__toString();
130130
if (self::isDebugMode()) {
131-
$this->getLogger()->debug(sprintf('Received a WebSocket message: %s', $msgStr));
131+
static::getLogger()->debug(sprintf('Received a WebSocket message: %s', $msgStr));
132132
}
133133
$msgArray = json_decode($msgStr, true);
134134
if (!isset($msgArray['type'])) {

0 commit comments

Comments
 (0)