slog移动到核心think包 将来进一步改进

This commit is contained in:
thinkphp
2015-12-10 15:23:24 +08:00
parent 7b0fe7286d
commit 159d425cd0
6 changed files with 7 additions and 568 deletions

View File

@@ -1,382 +0,0 @@
<?php
/**
* github: https://github.com/luofei614/SocketLog
* @author luofei614<weibo.com/luofei614>
*/
namespace org;
use think\Exception;
class Slog
{
public static $start_time = 0;
public static $start_memory = 0;
public static $port = 1116; //SocketLog 服务的http的端口号
public static $log_types = ['log', 'info', 'error', 'warn', 'table', 'group', 'groupCollapsed', 'groupEnd', 'alert'];
protected static $config = [
'enable' => true, //是否记录日志的开关
'host' => 'localhost',
//是否显示利于优化的参数,如果允许时间,消耗内存等
'optimize' => false,
'show_included_files' => false,
'error_handler' => false,
//日志强制记录到配置的client_id
'force_client_id' => '',
//限制允许读取日志的client_id
'allow_client_ids' => [],
];
protected static $logs = [];
protected static $css = [
'sql' => 'color:#009bb4;',
'sql_warn' => 'color:#009bb4;font-size:14px;',
'error_handler' => 'color:#f4006b;font-size:14px;',
'page' => 'color:#40e2ff;background:#171717;',
'big' => 'font-size:20px;color:red;',
];
public static function sql($sql, $pdo)
{
if (is_object($pdo)) {
if (!self::check()) {
return;
}
$css = self::$css['sql'];
if (preg_match('/^SELECT /i', $sql)) {
//explain
try {
$obj = $pdo->query("EXPLAIN " . $sql);
if (is_object($obj) && method_exists($obj, 'fetch')) {
$arr = $obj->fetch(\PDO::FETCH_ASSOC);
self::sqlexplain($arr, $sql, $css);
}
} catch (Exception $e) {
}
}
self::sqlwhere($sql, $css);
self::trace($sql, 2, $css);
}else{
throw new Exception('SocketLog can not support this database link');
}
}
public static function trace($msg, $trace_level = 2, $css = '')
{
if (!self::check()) {
return;
}
self::groupCollapsed($msg, $css);
$traces = debug_backtrace(false);
$traces = array_reverse($traces);
$max = count($traces) - $trace_level;
for ($i = 0; $i < $max; $i++) {
$trace = $traces[$i];
$fun = isset($trace['class']) ? $trace['class'] . '::' . $trace['function'] : $trace['function'];
$file = isset($trace['file']) ? $trace['file'] : 'unknown file';
$line = isset($trace['line']) ? $trace['line'] : 'unknown line';
$trace_msg = '#' . $i . ' ' . $fun . ' called at [' . $file . ':' . $line . ']';
if (!empty($trace['args'])) {
self::record('groupCollapsed',$trace_msg);
self::record('log',$trace['args']);
self::record('groupEnd');
} else {
self::record('log',$trace_msg);
}
}
self::record('groupEnd');
}
private static function sqlexplain($arr, &$sql, &$css)
{
$arr = array_change_key_case($arr, CASE_LOWER);
if (false !== strpos($arr['extra'], 'Using filesort')) {
$sql .= ' <---################[Using filesort]';
$css = self::$css['sql_warn'];
}
if (false !== strpos($arr['extra'], 'Using temporary')) {
$sql .= ' <---################[Using temporary]';
$css = self::$css['sql_warn'];
}
}
private static function sqlwhere(&$sql, &$css)
{
//判断sql语句是否有where
if (preg_match('/^UPDATE |DELETE /i', $sql) && !preg_match('/WHERE.*(=|>|<|LIKE|IN)/i', $sql)) {
$sql .= '<---###########[NO WHERE]';
$css = self::$css['sql_warn'];
}
}
/**
* 接管报错
*/
public static function registerErrorHandler()
{
if (!self::check()) {
return;
}
set_error_handler([__CLASS__, 'error_handler']);
register_shutdown_function([__CLASS__, 'fatalError']);
}
public static function error_handler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_WARNING:
$severity = 'E_WARNING';
break;
case E_NOTICE:
$severity = 'E_NOTICE';
break;
case E_USER_ERROR:
$severity = 'E_USER_ERROR';
break;
case E_USER_WARNING:
$severity = 'E_USER_WARNING';
break;
case E_USER_NOTICE:
$severity = 'E_USER_NOTICE';
break;
case E_STRICT:
$severity = 'E_STRICT';
break;
case E_RECOVERABLE_ERROR:
$severity = 'E_RECOVERABLE_ERROR';
break;
case E_DEPRECATED:
$severity = 'E_DEPRECATED';
break;
case E_USER_DEPRECATED:
$severity = 'E_USER_DEPRECATED';
break;
case E_ERROR:
$severity = 'E_ERR';
break;
case E_PARSE:
$severity = 'E_PARSE';
break;
case E_CORE_ERROR:
$severity = 'E_CORE_ERROR';
break;
case E_COMPILE_ERROR:
$severity = 'E_COMPILE_ERROR';
break;
case E_USER_ERROR:
$severity = 'E_USER_ERROR';
break;
default:
$severity = 'E_UNKNOWN_ERROR_' . $errno;
break;
}
$msg = "{$severity}: {$errstr} in {$errfile} on line {$errline} -- SocketLog error handler";
self::trace($msg, 2, self::$css['error_handler']);
}
public static function fatalError()
{
// 保存日志记录
if ($e = error_get_last()) {
self::error_handler($e['type'], $e['message'], $e['file'], $e['line']);
self::sendLog();
}
}
protected static function check()
{
if (!SLOG_ON) {
return false;
}
$tabid = self::getClientArg('tabid');
//是否记录日志的检查
if (!$tabid && !self::$config['force_client_id']) {
return false;
}
//用户认证
$allow_client_ids = self::$config['allow_client_ids'];
if (!empty($allow_client_ids)) {
if (!$tabid && in_array(self::$config['force_client_id'], $allow_client_ids)) {
return true;
}
$client_id = self::getClientArg('client_id');
if (!in_array($client_id, $allow_client_ids)) {
return false;
}
}
return true;
}
protected static function getClientArg($name)
{
static $args = [];
$key = 'HTTP_USER_AGENT';
if (isset($_SERVER['HTTP_SOCKETLOG'])) {
$key = 'HTTP_SOCKETLOG';
}
if (!isset($_SERVER[$key])) {
return null;
}
if (empty($args)) {
if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
$args = ['tabid' => null];
return null;
}
parse_str($match[1], $args);
}
if (isset($args[$name])) {
return $args[$name];
}
return null;
}
//设置配置
public static function config($config)
{
$config = array_merge(self::$config, $config);
self::$config = $config;
if (self::check()) {
if ($config['optimize']) {
self::$start_time = microtime(true);
self::$start_memory = memory_get_usage();
}
if ($config['error_handler']) {
self::registerErrorHandler();
}
}
}
//获得配置
public static function getConfig($name)
{
return isset(self::$config[$name]) ? self::$config[$name] : null;
}
//记录日志
public static function record($type, $msg = '', $css = '')
{
if (!self::check()) {
return;
}
self::$logs[] = [
'type' => $type,
'msg' => $msg,
'css' => isset(self::$css[$css]) ? self::$css[$css] : $css,
];
}
/**
* @param null $host - $host of socket server
* @param string $message - 发送的消息
* @param string $address - 地址
* @return bool
*/
public static function send($host, $message = '', $address = '/')
{
$url = 'http://' . $host . ':' . self::$port . $address;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$headers = [
"Content-Type: application/json;charset=UTF-8",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
$txt = curl_exec($ch);
return true;
}
public static function sendLog()
{
if (!self::check()) {
return;
}
$time_str = '';
$memory_str = '';
if (self::$start_time) {
$runtime = microtime(true) - self::$start_time;
$reqs = number_format(1 / $runtime, 2);
$time_str = "[运行时间:{$runtime}s][吞吐率:{$reqs}req/s]";
}
if (self::$start_memory) {
$memory_use = number_format(memory_get_usage() - self::$start_memory / 1024, 2);
$memory_str = "[内存消耗:{$memory_use}kb]";
}
if (isset($_SERVER['HTTP_HOST'])) {
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
}
array_unshift(self::$logs, [
'type' => 'group',
'msg' => $current_uri . $time_str . $memory_str,
'css' => self::$css['page'],
]);
if (self::$config['show_included_files']) {
self::$logs[] = [
'type' => 'groupCollapsed',
'msg' => 'included_files',
'css' => '',
];
self::$logs[] = [
'type' => 'log',
'msg' => implode("\n", get_included_files()),
'css' => '',
];
self::$logs[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
}
self::$logs[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
$tabid = self::getClientArg('tabid');
if (!$client_id = self::getClientArg('client_id')) {
$client_id = '';
}
if ($force_client_id = self::$config['force_client_id']) {
$client_id = $force_client_id;
}
$logs = [
'tabid' => $tabid,
'client_id' => $client_id,
'logs' => self::$logs,
'force_client_id' => $force_client_id,
];
$msg = @json_encode($logs);
$address = '/' . $client_id; //将client_id作为地址 server端通过地址判断将日志发布给谁
self::send(self::$config['host'], $msg, $address);
}
public static function __callStatic($method, $args)
{
if (in_array($method, self::$log_types)) {
array_unshift($args, $method);
return call_user_func_array(['\org\Slog','record'], $args);
}
}
}

View File

@@ -1,560 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace org;
use org\Image;
class Upload
{
protected $config = [
'max_size' => -1, // 上传文件的最大值
'support_multi' => true, // 是否支持多文件上传
'allow_exts' => [], // 允许上传的文件后缀 留空不作后缀检查
'allow_types' => [], // 允许上传的文件类型 留空不做检查
'thumb' => false, // 使用对上传图片进行缩略图处理
'thumb_max_width' => '', // 缩略图最大宽度
'thumb_max_height' => '', // 缩略图最大高度
'thumb_prefix' => 'thumb_', // 缩略图前缀
'thumb_suffix' => '',
'thumb_path' => '', // 缩略图保存路径
'thumb_file' => '', // 缩略图文件名
'thumb_ext' => '', // 缩略图扩展名
'thumb_remove_origin' => false, // 是否移除原图
'zip_images' => false, // 压缩图片文件上传
'auto_sub' => false, // 启用子目录保存文件
'sub_type' => 'hash', // 子目录创建方式 可以使用hash date custom
'sub_dir' => '', // 子目录名称 subType为custom方式后有效
'date_format' => 'Ymd',
'hash_level' => 1, // hash的目录层次
'save_path' => '', // 上传文件保存路径
'auto_check' => true, // 是否自动检查附件
'upload_replace' => false, // 存在同名是否覆盖
'save_rule' => 'uniqid', // 上传文件命名规则
'hash_type' => 'md5_file', // 上传文件Hash规则函数名
];
// 错误信息
private $error = '';
// 上传成功的文件信息
private $uploadFileInfo;
public function __get($name)
{
if (isset($this->config[$name])) {
return $this->config[$name];
}
return null;
}
public function __set($name, $value)
{
if (isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
public function __isset($name)
{
return isset($this->config[$name]);
}
/**
* 架构函数
* @access public
* @param array $config 上传参数
*/
public function __construct($config = [])
{
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
}
}
/**
* 上传一个文件
* @access protected
* @param mixed $name 数据
* @param string $value 数据表名
* @return string
*/
protected function save($file)
{
$filename = $file['save_path'] . $file['savename'];
if (!$this->upload_replace && is_file($filename)) {
// 不覆盖同名文件
$this->error = '文件已经存在!' . $filename;
return false;
}
// 如果是图像文件 检测文件格式
if (in_array(strtolower($file['extension']), ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) {
$info = getimagesize($file['tmp_name']);
if (false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))) {
$this->error = '非法图像文件';
return false;
}
}
if (!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename, 'utf-8', 'gbk'))) {
$this->error = '文件上传保存错误!';
return false;
}
if ($this->thumb && in_array(strtolower($file['extension']), ['gif', 'jpg', 'jpeg', 'bmp', 'png'])) {
$image = getimagesize($filename);
if (false !== $image) {
//是图像文件生成缩略图
$thumbWidth = explode(',', $this->thumb_max_width);
$thumbHeight = explode(',', $this->thumb_max_height);
$thumb_prefix = explode(',', $this->thumb_prefix);
$thumb_suffix = explode(',', $this->thumb_suffix);
$thumb_file = explode(',', $this->thumb_file);
$thumb_path = $this->thumb_path ? $this->thumb_path : dirname($filename) . '/';
$thumb_ext = $this->thumb_ext ? $this->thumb_ext : $file['extension']; //自定义缩略图扩展名
// 生成图像缩略图
for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) {
if (!empty($thumb_file[$i])) {
$thumbname = $thumb_file[$i];
} else {
$prefix = isset($thumb_prefix[$i]) ? $thumb_prefix[$i] : $thumb_prefix[0];
$suffix = isset($thumb_suffix[$i]) ? $thumb_suffix[$i] : $thumb_suffix[0];
$thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix;
}
Image::thumb($filename, $thumb_path . $thumbname . '.' . $thumb_ext, '', $thumbWidth[$i], $thumbHeight[$i], true);
}
if ($this->thumb_remove_origin) {
// 生成缩略图之后删除原图
unlink($filename);
}
}
}
if ($this->zipImags) {
// TODO 对图片压缩包在线解压
}
return true;
}
/**
* 上传所有文件
* @access public
* @param string $savePath 上传文件保存路径
* @return string
*/
public function upload($savePath = '')
{
//如果不指定保存文件名,则由系统默认
if (empty($savePath)) {
$savePath = $this->save_path;
}
// 检查上传目录
if (!is_dir($savePath)) {
// 检查目录是否编码后的
if (is_dir(base64_decode($savePath))) {
$savePath = base64_decode($savePath);
} else {
// 尝试创建目录
if (!mkdir($savePath)) {
$this->error = '上传目录' . $savePath . '不存在';
return false;
}
}
} else {
if (!is_writeable($savePath)) {
$this->error = '上传目录' . $savePath . '不可写';
return false;
}
}
$fileInfo = [];
$isUpload = false;
// 获取上传的文件信息
// 对$_FILES数组信息处理
$files = $this->dealFiles($_FILES);
foreach ($files as $key => $file) {
//过滤无效的上传
if (!empty($file['name'])) {
//登记上传文件的扩展信息
if (!isset($file['key'])) {
$file['key'] = $key;
}
$file['extension'] = $this->getExt($file['name']);
$file['savepath'] = $savePath;
$file['savename'] = $this->getSaveName($file);
// 自动检查附件
if ($this->auto_check) {
if (!$this->check($file)) {
return false;
}
}
//保存上传文件
if (!$this->save($file)) {
return false;
}
if (function_exists($this->hash_type)) {
$fun = $this->hash_type;
$file['hash'] = $fun($this->autoCharset($file['savepath'] . $file['savename'], 'utf-8', 'gbk'));
}
//上传成功后保存文件信息,供其他地方调用
unset($file['tmp_name'], $file['error']);
$fileInfo[] = $file;
$isUpload = true;
}
}
if ($isUpload) {
$this->uploadFileInfo = $fileInfo;
return true;
} else {
$this->error = '没有选择上传文件';
return false;
}
}
/**
* 上传单个上传字段中的文件 支持多附件
* @access public
* @param array $file 上传文件信息
* @param string $savePath 上传文件保存路径
* @return string
*/
public function uploadOne($file, $savePath = '')
{
//如果不指定保存文件名,则由系统默认
if (empty($savePath)) {
$savePath = $this->save_path;
}
// 检查上传目录
if (!is_dir($savePath)) {
// 尝试创建目录
if (!mkdir($savePath, 0777, true)) {
$this->error = '上传目录' . $savePath . '不存在';
return false;
}
} else {
if (!is_writeable($savePath)) {
$this->error = '上传目录' . $savePath . '不可写';
return false;
}
}
//过滤无效的上传
if (!empty($file['name'])) {
$fileArray = [];
if (is_array($file['name'])) {
$keys = array_keys($file);
$count = count($file['name']);
for ($i = 0; $i < $count; $i++) {
foreach ($keys as $key) {
$fileArray[$i][$key] = $file[$key][$i];
}
}
} else {
$fileArray[] = $file;
}
$info = [];
foreach ($fileArray as $key => $file) {
//登记上传文件的扩展信息
$file['extension'] = $this->getExt($file['name']);
$file['savepath'] = $savePath;
$file['savename'] = $this->getSaveName($file);
// 自动检查附件
if ($this->auto_check) {
if (!$this->check($file)) {
return false;
}
}
//保存上传文件
if (!$this->save($file)) {
return false;
}
if (function_exists($this->hash_type)) {
$fun = $this->hash_type;
$file['hash'] = $fun($this->autoCharset($file['savepath'] . $file['savename'], 'utf-8', 'gbk'));
}
unset($file['tmp_name'], $file['error']);
$info[] = $file;
}
// 返回上传的文件信息
return $info;
} else {
$this->error = '没有选择上传文件';
return false;
}
}
/**
* 转换上传文件数组变量为正确的方式
* @access protected
* @param array $files 上传的文件变量
* @return array
*/
protected function dealFiles($files)
{
$fileArray = [];
$n = 0;
foreach ($files as $key => $file) {
if (is_array($file['name'])) {
$keys = array_keys($file);
$count = count($file['name']);
for ($i = 0; $i < $count; $i++) {
$fileArray[$n]['key'] = $key;
foreach ($keys as $_key) {
$fileArray[$n][$_key] = $file[$_key][$i];
}
$n++;
}
} else {
$fileArray[$key] = $file;
}
}
return $fileArray;
}
/**
* 获取错误代码信息
* @access public
* @param string $errorNo 错误号码
* @return void
*/
protected function error($errorNo)
{
switch ($errorNo) {
case 1:
$this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
$this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
$this->error = '文件只有部分被上传';
break;
case 4:
$this->error = '没有文件被上传';
break;
case 6:
$this->error = '找不到临时文件夹';
break;
case 7:
$this->error = '文件写入失败';
break;
default:
$this->error = '未知上传错误!';
}
return;
}
/**
* 根据上传文件命名规则取得保存文件名
* @access protected
* @param string $filename 数据
* @return string
*/
protected function getSaveName($filename)
{
$rule = $this->save_rule;
if (empty($rule)) {
//没有定义命名规则,则保持文件名不变
$saveName = $filename['name'];
} else {
if (function_exists($rule)) {
//使用函数生成一个唯一文件标识号
$saveName = $rule() . "." . $filename['extension'];
} else {
//使用给定的文件名作为标识号
$saveName = $rule . "." . $filename['extension'];
}
}
if ($this->auto_sub) {
// 使用子目录保存文件
$filename['savename'] = $saveName;
$saveName = $this->getSubName($filename) . $saveName;
}
return $saveName;
}
/**
* 获取子目录的名称
* @access protected
* @param array $file 上传的文件信息
* @return string
*/
protected function getSubName($file)
{
switch ($this->sub_type) {
case 'custom':
$dir = $this->sub_dir;
break;
case 'date':
$dir = date($this->date_format, time()) . '/';
break;
case 'hash':
default:
$name = md5($file['savename']);
$dir = '';
for ($i = 0; $i < $this->hash_level; $i++) {
$dir .= $name{$i} . '/';
}
break;
}
if (!is_dir($file['savepath'] . $dir)) {
mkdir($file['savepath'] . $dir, 0777, true);
}
return $dir;
}
/**
* 检查上传的文件
* @access protected
* @param array $file 文件信息
* @return boolean
*/
protected function check($file)
{
if (0 !== $file['error']) {
//文件上传失败
//捕获错误代码
$this->error($file['error']);
return false;
}
//文件上传成功,进行自定义规则检查
//检查文件大小
if (!$this->checkSize($file['size'])) {
$this->error = '上传文件大小不符!';
return false;
}
//检查文件Mime类型
if (!$this->checkType($file['type'])) {
$this->error = '上传文件MIME类型不允许';
return false;
}
//检查文件类型
if (!$this->checkExt($file['extension'])) {
$this->error = '上传文件类型不允许';
return false;
}
//检查是否合法上传
if (!$this->checkUpload($file['tmp_name'])) {
$this->error = '非法上传文件!';
return false;
}
return true;
}
// 自动转换字符集 支持数组转换
protected function autoCharset($fContents, $from = 'gbk', $to = 'utf-8')
{
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
//如果编码相同或者非字符串标量则不转换
return $fContents;
}
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($fContents, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
}
/**
* 检查上传的文件类型是否合法
* @access protected
* @param string $type 数据
* @return boolean
*/
protected function checkType($type)
{
if (!empty($this->allow_types)) {
return in_array(strtolower($type), $this->allow_types);
}
return true;
}
/**
* 检查上传的文件后缀是否合法
* @access protected
* @param string $ext 后缀名
* @return boolean
*/
protected function checkExt($ext)
{
if (!empty($this->allow_exts)) {
return in_array(strtolower($ext), $this->allow_exts, true);
}
return true;
}
/**
* 检查文件大小是否合法
* @access protected
* @param integer $size 数据
* @return boolean
*/
protected function checkSize($size)
{
return !($size > $this->max_size) || (-1 == $this->max_size);
}
/**
* 检查文件是否非法提交
* @access protected
* @param string $filename 文件名
* @return boolean
*/
protected function checkUpload($filename)
{
return is_uploaded_file($filename);
}
/**
* 取得上传文件的后缀
* @access protected
* @param string $filename 文件名
* @return boolean
*/
protected function getExt($filename)
{
return pathinfo($filename, PATHINFO_EXTENSION);
}
/**
* 取得上传文件的信息
* @access public
* @return array
*/
public function getUploadFileInfo()
{
return $this->uploadFileInfo;
}
/**
* 取得最后一次错误信息
* @access public
* @return string
*/
public function getErrorMsg()
{
return $this->error;
}
}