-
Notifications
You must be signed in to change notification settings - Fork 54
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
宋神宗
authored
Mar 27, 2019
1 parent
2b9d2f0
commit c8405ac
Showing
9 changed files
with
150 additions
and
13 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
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
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
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
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,77 @@ | ||
<?php | ||
|
||
namespace AlibabaCloud\Client\Log; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Exception; | ||
use GuzzleHttp\MessageFormatter; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class LogFormatter | ||
* | ||
* @package AlibabaCloud\Client\Log | ||
*/ | ||
class LogFormatter extends MessageFormatter | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
private static $logStartTime = 0; | ||
/** | ||
* @var DateTime | ||
*/ | ||
private static $ts; | ||
|
||
/** @var string Template used to format log messages */ | ||
public $template; | ||
|
||
/** | ||
* @param string $template Log message template | ||
* | ||
* @throws Exception | ||
*/ | ||
public function __construct($template) | ||
{ | ||
parent::__construct($template); | ||
self::$logStartTime = microtime(true); | ||
$this->template = $template; | ||
$timezone = new DateTimeZone(date_default_timezone_get() ?: 'UTC'); | ||
if (PHP_VERSION_ID < 70100) { | ||
self::$ts = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone); | ||
} else { | ||
self::$ts = new DateTime(null, $timezone); | ||
} | ||
} | ||
|
||
/** | ||
* @return float|mixed | ||
*/ | ||
private static function getCost() | ||
{ | ||
return microtime(true) - self::$logStartTime; | ||
} | ||
|
||
/** | ||
* Returns a formatted message string. | ||
* | ||
* @param RequestInterface $request Request that was sent | ||
* @param ResponseInterface $response Response that was received | ||
* @param Exception $error Exception that was received | ||
* | ||
* @return string | ||
*/ | ||
public function format( | ||
RequestInterface $request, | ||
ResponseInterface $response = null, | ||
Exception $error = null | ||
) { | ||
$this->template = str_replace('{pid}', getmypid(), $this->template); | ||
$this->template = str_replace('{cost}', self::getCost(), $this->template); | ||
$this->template = str_replace('{start_time}', self::$ts->format('Y-m-d H:i:s.u'), $this->template); | ||
|
||
return (new MessageFormatter($this->template))->format($request, $response, $error); | ||
} | ||
} |
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
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
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,52 @@ | ||
<?php | ||
|
||
namespace AlibabaCloud\Client\Tests\Mock; | ||
|
||
use org\bovigo\vfs\vfsStream; | ||
|
||
/** | ||
* Class VirtualFile | ||
* | ||
* @package AlibabaCloud\Client\Tests\Mock | ||
*/ | ||
class VirtualFile | ||
{ | ||
|
||
/** | ||
* @var string VirtualFile Content | ||
*/ | ||
private $content; | ||
|
||
/** | ||
* VirtualFile constructor. | ||
* | ||
* @param string $content | ||
*/ | ||
private function __construct($content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
/** | ||
* @param string $content | ||
* | ||
* @return VirtualFile | ||
*/ | ||
public static function content($content = '') | ||
{ | ||
return new static($content); | ||
} | ||
|
||
/** | ||
* @param string $fileName | ||
* | ||
* @return string VirtualFile Filename | ||
*/ | ||
public function url($fileName = 'file') | ||
{ | ||
return vfsStream::newFile($fileName) | ||
->withContent($this->content) | ||
->at(vfsStream::setup('AlibabaCloud')) | ||
->url(); | ||
} | ||
} |
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