// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st
// +----------------------------------------------------------------------
namespacelog;uselogConfig;usethink\App;usethink\Request;usethink\Db;/**
* 本地化调试输出到文件*/
class Mysql{//实例化并传入参数
public function __construct($config =[])
{
}/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @param bool $append 是否追加请求信息
* @return bool*/
public function save(array $log = [], $append = false)
{$info =[];$data =[];foreach ($log as $type => $val) {foreach ($val as $msg) {if (!is_string($msg)) {$msg = var_export($msg, true);
}$info[$type][] = $msg;
}
}if ($info) {$request = Request::instance();//将日志写入数据库
$data["ip"]=$request->ip();$data["method"]=$request->method();$data["url"]=$request->host().$request->url();$data["info"]=$this->write($info, false, $append);
Db::table("log")->insert($data);
}return true;
}/**
* 日志写入
* @access protected
* @param array $message 日志信息
* @param string $destination 日志文件
* @param bool $apart 是否独立文件写入
* @param bool $append 是否追加请求信息
* @return bool*/
protected function write($message, $apart = false, $append = false)
{//日志信息封装
$info['timestamp'] = date(' c ');foreach ($message as $type => $msg) {$info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
}if (PHP_SAPI == 'cli') {$message = $this->parseCliLog($info);
}else{//添加调试日志
$this->getDebugLog($info, $append, $apart);$message = $this->parseLog($info);
}return $message;
}/**
* CLI日志解析
* @access protected
* @param array $info 日志信息
* @return string*/
protected function parseCliLog($info)
{$now = $info['timestamp'];unset($info['timestamp']);$message = implode("\r\n", $info);$message = "[{$now}]" . $message . "\r\n";return $message;
}/**
* 解析日志
* @access protected
* @param array $info 日志信息
* @return string*/
protected function parseLog($info)
{$request = Request::instance();$requestInfo =['ip' => $request->ip(),
'method' => $request->method(),
'host' => $request->host(),
'uri' => $request->url(),];array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");unset($info['timestamp']);return implode("\r\n", $info) . "\r\n";
}protected function getDebugLog(&$info, $append, $apart)
{if (App::$debug && $append) {//增加额外的调试信息
$runtime = round(microtime(true) - THINK_START_TIME, 10);$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);$time_str = '[运行时间:' . number_format($runtime, 6) . 's] [吞吐率:' . $reqs . 'req/s]';$memory_str = ' [内存消耗:' . $memory_use . 'kb]';$file_load = ' [文件加载:' . count(get_included_files()) . ']';array_unshift($info, $time_str . $memory_str . $file_load);
}
}
}