PHP code example of apeipo / xl-util

1. Go to this page and download the library: Download apeipo/xl-util library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apeipo / xl-util example snippets


XLCost::begin("costA");
usleep(100 * 1000); //100ms
XLCost::end("costA");

XLCost::begin("costB");
usleep(200 * 1000); //100ms
XLCost::end("costB");

#数组返回
print_r(XLCost::all());

#str返回
print(XLCost::str() . "\n");

# 循环中的耗时计算, 超过一次的项会计算平均耗时
//test repeat
for($i = 0; $i < 5; $i++) {
	XLCost::begin("costRepeat");
	usleep(50 * 1000);
	
	XLCost::begin("costInRepeat");
	usleep(80 * 1000);
	XLCost::end("costInRepeat");
	XLCost::end("costRepeat");
}
print_r("Test Repeat\n");
print_r(XLCost::flush());

use XLUtil\XLLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$handler = (new StreamHandler("./test.log", Logger::DEBUG));
$logger  = new Logger('test', [$handler]);

XLLog::setLogger($logger);
#设置全局logid, 这个逻辑可以放在web框架的入口,生成logid的方式请自行修改
XLLog::setLogid("LOG_ID", time());

XLLog::addNoticeKV("file", __FILE__); #增加key,但是不打印
XLLog::addNoticeKV("method", "test");
XLLog::warning("This is in test");
XLLog::flush("RequestFinish");  #打印所有的kv并清空

####output
[2017-07-10 13:22:56] test.WARNING: This is in test {"LOG_ID":1499692976} []
[2017-07-10 13:22:56] test.NOTICE: RequestDone {"file":"testMonologDecorator.php","method":"test","LOG_ID":1499692976} []