From 9496e4a42ec84262890e9a971661272dac815679 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 29 Feb 2016 10:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Log=E7=B1=BB=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Log.php | 17 ++++++-- library/think/log/driver/File.php | 3 +- library/think/log/driver/Sae.php | 4 +- library/think/log/driver/Socket.php | 10 ++++- library/think/log/driver/Test.php | 3 +- library/think/log/driver/Trace.php | 5 ++- tests/thinkphp/library/think/logTest.php | 48 +++++++++++++++++++++ tests/thinkphp/library/think/urlTest.php | 55 +++++++++++++----------- 8 files changed, 108 insertions(+), 37 deletions(-) create mode 100644 tests/thinkphp/library/think/logTest.php diff --git a/library/think/Log.php b/library/think/Log.php index cf7c13a8..55290d30 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -75,22 +75,31 @@ class Log } /** - * 保存调试信息 + * 清空日志信息 * @return void */ + public static function clear() + { + self::$log = []; + } + + /** + * 保存调试信息 + * @return bool + */ public static function save() { if (is_null(self::$driver)) { self::init(Config::get('log')); } - self::$driver->save(self::$log); + return self::$driver->save(self::$log); } /** * 实时写入日志信息 并支持行为 * @param mixed $msg 调试信息 * @param string $type 信息类型 - * @return void + * @return bool */ public static function write($msg, $type = 'log') { @@ -106,7 +115,7 @@ class Log self::init(Config::get('log')); } // 写入日志 - self::$driver->save($log); + return self::$driver->save($log); } /** diff --git a/library/think/log/driver/File.php b/library/think/log/driver/File.php index 654c3282..484e577b 100644 --- a/library/think/log/driver/File.php +++ b/library/think/log/driver/File.php @@ -33,7 +33,7 @@ class File * 日志写入接口 * @access public * @param array $log 日志信息 - * @return void + * @return bool */ public function save(array $log = []) { @@ -74,6 +74,7 @@ class File $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; error_log("[{$now}] {$server} {$remote} {$uri}\r\n{$info}\r\n", 3, $destination); + return true; } } diff --git a/library/think/log/driver/Sae.php b/library/think/log/driver/Sae.php index 01616083..c50d18f4 100644 --- a/library/think/log/driver/Sae.php +++ b/library/think/log/driver/Sae.php @@ -29,7 +29,7 @@ class Sae * 日志写入接口 * @access public * @param array $log 日志信息 - * @return void + * @return bool */ public function save(array $log = []) { @@ -73,7 +73,7 @@ class Sae if ($is_debug) { sae_set_display_errors(true); } - + return true; } } diff --git a/library/think/log/driver/Socket.php b/library/think/log/driver/Socket.php index 25d27f5e..00843774 100644 --- a/library/think/log/driver/Socket.php +++ b/library/think/log/driver/Socket.php @@ -48,10 +48,16 @@ class Socket } } + /** + * 日志写入接口 + * @access public + * @param array $logs 日志信息 + * @return bool + */ public function save(array $logs = []) { if (!$this->check()) { - return; + return false; } $runtime = number_format(microtime(true) - START_TIME, 6); $reqs = number_format(1 / $runtime, 2); @@ -112,7 +118,7 @@ class Socket } else { $this->sendToClient($tabid, $client_id, $logs, ''); } - + return true; } /** diff --git a/library/think/log/driver/Test.php b/library/think/log/driver/Test.php index f51a232d..e367ca3c 100644 --- a/library/think/log/driver/Test.php +++ b/library/think/log/driver/Test.php @@ -19,10 +19,11 @@ class Test * 日志写入接口 * @access public * @param array $log 日志信息 - * @return void + * @return bool */ public function save(array $log = []) { + return true; } } diff --git a/library/think/log/driver/Trace.php b/library/think/log/driver/Trace.php index 7b4f5b73..27695d39 100644 --- a/library/think/log/driver/Trace.php +++ b/library/think/log/driver/Trace.php @@ -34,13 +34,13 @@ class Trace * 日志写入接口 * @access public * @param array $log 日志信息 - * @return void + * @return bool */ public function save(array $log = []) { if (IS_AJAX || IS_CLI || IS_API || 'html' != Config::get('default_return_type')) { // ajax cli api方式下不输出 - return; + return false; } // 获取基本信息 $runtime = number_format(microtime(true) - START_TIME, 6); @@ -94,6 +94,7 @@ class Trace ob_start(); include $this->config['trace_file']; echo ob_get_clean(); + return true; } } diff --git a/tests/thinkphp/library/think/logTest.php b/tests/thinkphp/library/think/logTest.php new file mode 100644 index 00000000..3448b49e --- /dev/null +++ b/tests/thinkphp/library/think/logTest.php @@ -0,0 +1,48 @@ + +// +---------------------------------------------------------------------- + +/** + * Log测试 + * @author liu21st + */ +namespace tests\thinkphp\library\think; + +use think\Log; + +class logTest extends \PHPUnit_Framework_TestCase +{ + + public function testRecord(){ + Log::clear(); + Log::record('test'); + $this->assertEquals([['type'=>'log','msg'=>'test']], Log::getLog()); + Log::record('hello','info'); + $this->assertEquals([['type'=>'log','msg'=>'test'],['type'=>'info','msg'=>'hello']], Log::getLog()); + Log::clear(); + Log::info('test'); + $this->assertEquals([['type'=>'info','msg'=>'test']], Log::getLog()); + } + + public function testSave(){ + Log::init(['type'=>'test']); + Log::clear(); + Log::record('test'); + Log::record([1,2,3]); + $this->assertTrue(Log::save()); + } + + public function testWrite(){ + Log::init(['type'=>'test']); + Log::clear(); + $this->assertTrue(Log::write('hello','info')); + $this->assertTrue(Log::write([1,2,3],'log')); + } +} diff --git a/tests/thinkphp/library/think/urlTest.php b/tests/thinkphp/library/think/urlTest.php index 43b83ac2..f41c13b2 100644 --- a/tests/thinkphp/library/think/urlTest.php +++ b/tests/thinkphp/library/think/urlTest.php @@ -10,50 +10,55 @@ // +---------------------------------------------------------------------- /** - * Route测试 + * Url测试 * @author liu21st */ namespace tests\thinkphp\library\think; -use think\Url; -use think\Route; use think\Config; +use think\Route; +use think\Url; class urlTest extends \PHPUnit_Framework_TestCase { - public function testBuildModule(){ - - Route::get('hello/:name','index/hello'); - Route::get('hello/:id','index/hello'); - Config::set('pathinfo_depr','/'); - $this->assertEquals('/hello/thinkphp',Url::build('index/hello?name=thinkphp')); - $this->assertEquals('/hello/thinkphp.html',Url::build('index/hello','name=thinkphp','html')); - $this->assertEquals('/hello/10',Url::build('index/hello?id=10')); - $this->assertEquals('/hello/10.html',Url::build('index/hello','id=10','html')); + public function testBuildModule() + { + + Route::get('hello/:name', 'index/hello'); + Route::get('hello/:id', 'index/hello'); + Config::set('pathinfo_depr', '/'); + $this->assertEquals('/hello/thinkphp', Url::build('index/hello?name=thinkphp')); + $this->assertEquals('/hello/thinkphp.html', Url::build('index/hello', 'name=thinkphp', 'html')); + $this->assertEquals('/hello/10', Url::build('index/hello?id=10')); + $this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html')); } - public function testBuildController(){ - Route::get('blog/:id','@index/blog/read'); - $this->assertEquals('/blog/10.html',Url::build('@index/blog/read','id=10','html')); + public function testBuildController() + { + Route::get('blog/:id', '@index/blog/read'); + $this->assertEquals('/blog/10.html', Url::build('@index/blog/read', 'id=10', 'html')); } - public function testBuildMethod(){ - Route::get('blog/:id', ['\app\index\controller\blog','read']); - $this->assertEquals('/blog/10.html',Url::build('\app\index\controller\blog\read','id=10','html')); + public function testBuildMethod() + { + Route::get('blog/:id', ['\app\index\controller\blog', 'read']); + $this->assertEquals('/blog/10.html', Url::build('\app\index\controller\blog\read', 'id=10', 'html')); } - public function testBuildRoute(){ + public function testBuildRoute() + { Route::get('blog/:id', 'index/blog'); - Config::set('url_html_suffix','shtml'); - $this->assertNotEquals('/blog/10.html',Url::build('/blog/10')); - $this->assertEquals('/blog/10.shtml',Url::build('/blog/10')); + Config::set('url_html_suffix', 'shtml'); + $this->assertNotEquals('/blog/10.html', Url::build('/blog/10')); + $this->assertEquals('/blog/10.shtml', Url::build('/blog/10')); } - public function testBuildAnchor(){ + public function testBuildAnchor() + { Route::get('blog/:id', 'index/blog'); - Config::set('url_html_suffix','shtml'); - $this->assertEquals('/blog/10.shtml#detail',Url::build('/blog/10#detail')); + Config::set('url_html_suffix', 'shtml'); + $this->assertEquals('/blog/10.shtml#detail', Url::build('/blog/10#detail')); } }