Socket.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: luofei614 <weibo.com/luofei614>
  10. // +----------------------------------------------------------------------
  11. namespace think\log\driver;
  12. /**
  13. * github: https://github.com/luofei614/SocketLog
  14. * @author luofei614<weibo.com/luofei614>
  15. */
  16. class Socket
  17. {
  18. public $port = 1116; //SocketLog 服务的http的端口号
  19. protected $config = [
  20. // socket服务器地址
  21. 'host' => 'localhost',
  22. // 是否显示加载的文件列表
  23. 'show_included_files' => false,
  24. // 日志强制记录到配置的client_id
  25. 'force_client_ids' => [],
  26. // 限制允许读取日志的client_id
  27. 'allow_client_ids' => [],
  28. // 调试开关
  29. 'debug' => false,
  30. ];
  31. protected $css = [
  32. 'sql' => 'color:#009bb4;',
  33. 'sql_warn' => 'color:#009bb4;font-size:14px;',
  34. 'error' => 'color:#f4006b;font-size:14px;',
  35. 'page' => 'color:#40e2ff;background:#171717;',
  36. 'big' => 'font-size:20px;color:red;',
  37. ];
  38. protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
  39. /**
  40. * 架构函数
  41. * @access public
  42. * @param array $config 缓存参数
  43. */
  44. public function __construct(array $config = [])
  45. {
  46. if (!empty($config)) {
  47. $this->config = array_merge($this->config, $config);
  48. }
  49. }
  50. /**
  51. * 调试输出接口
  52. * @access public
  53. * @param array $log 日志信息
  54. * @return bool
  55. */
  56. public function save(array $log = [])
  57. {
  58. if (!$this->check()) {
  59. return false;
  60. }
  61. $trace = [];
  62. if ($this->config['debug']) {
  63. if (isset($_SERVER['HTTP_HOST'])) {
  64. $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  65. } else {
  66. $current_uri = 'cmd:' . implode(' ', $_SERVER['argv']);
  67. }
  68. // 基本信息
  69. $trace[] = [
  70. 'type' => 'group',
  71. 'msg' => $current_uri,
  72. 'css' => $this->css['page'],
  73. ];
  74. }
  75. foreach ($log as $type => $val) {
  76. $trace[] = [
  77. 'type' => 'groupCollapsed',
  78. 'msg' => '[ ' . $type . ' ]',
  79. 'css' => isset($this->css[$type]) ? $this->css[$type] : '',
  80. ];
  81. foreach ($val as $msg) {
  82. if (!is_string($msg)) {
  83. $msg = var_export($msg, true);
  84. }
  85. $trace[] = [
  86. 'type' => 'log',
  87. 'msg' => $msg,
  88. 'css' => '',
  89. ];
  90. }
  91. $trace[] = [
  92. 'type' => 'groupEnd',
  93. 'msg' => '',
  94. 'css' => '',
  95. ];
  96. }
  97. if ($this->config['show_included_files']) {
  98. $trace[] = [
  99. 'type' => 'groupCollapsed',
  100. 'msg' => '[ file ]',
  101. 'css' => '',
  102. ];
  103. $trace[] = [
  104. 'type' => 'log',
  105. 'msg' => implode("\n", get_included_files()),
  106. 'css' => '',
  107. ];
  108. $trace[] = [
  109. 'type' => 'groupEnd',
  110. 'msg' => '',
  111. 'css' => '',
  112. ];
  113. }
  114. $trace[] = [
  115. 'type' => 'groupEnd',
  116. 'msg' => '',
  117. 'css' => '',
  118. ];
  119. $tabid = $this->getClientArg('tabid');
  120. if (!$client_id = $this->getClientArg('client_id')) {
  121. $client_id = '';
  122. }
  123. if (!empty($this->allowForceClientIds)) {
  124. //强制推送到多个client_id
  125. foreach ($this->allowForceClientIds as $force_client_id) {
  126. $client_id = $force_client_id;
  127. $this->sendToClient($tabid, $client_id, $trace, $force_client_id);
  128. }
  129. } else {
  130. $this->sendToClient($tabid, $client_id, $trace, '');
  131. }
  132. return true;
  133. }
  134. /**
  135. * 发送给指定客户端
  136. * @access protected
  137. * @author Zjmainstay
  138. * @param $tabid
  139. * @param $client_id
  140. * @param $logs
  141. * @param $force_client_id
  142. */
  143. protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
  144. {
  145. $logs = [
  146. 'tabid' => $tabid,
  147. 'client_id' => $client_id,
  148. 'logs' => $logs,
  149. 'force_client_id' => $force_client_id,
  150. ];
  151. $msg = @json_encode($logs);
  152. $address = '/' . $client_id; //将client_id作为地址, server端通过地址判断将日志发布给谁
  153. $this->send($this->config['host'], $msg, $address);
  154. }
  155. protected function check()
  156. {
  157. $tabid = $this->getClientArg('tabid');
  158. //是否记录日志的检查
  159. if (!$tabid && !$this->config['force_client_ids']) {
  160. return false;
  161. }
  162. //用户认证
  163. $allow_client_ids = $this->config['allow_client_ids'];
  164. if (!empty($allow_client_ids)) {
  165. //通过数组交集得出授权强制推送的client_id
  166. $this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
  167. if (!$tabid && count($this->allowForceClientIds)) {
  168. return true;
  169. }
  170. $client_id = $this->getClientArg('client_id');
  171. if (!in_array($client_id, $allow_client_ids)) {
  172. return false;
  173. }
  174. } else {
  175. $this->allowForceClientIds = $this->config['force_client_ids'];
  176. }
  177. return true;
  178. }
  179. protected function getClientArg($name)
  180. {
  181. static $args = [];
  182. $key = 'HTTP_USER_AGENT';
  183. if (isset($_SERVER['HTTP_SOCKETLOG'])) {
  184. $key = 'HTTP_SOCKETLOG';
  185. }
  186. if (!isset($_SERVER[$key])) {
  187. return;
  188. }
  189. if (empty($args)) {
  190. if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
  191. $args = ['tabid' => null];
  192. return;
  193. }
  194. parse_str($match[1], $args);
  195. }
  196. if (isset($args[$name])) {
  197. return $args[$name];
  198. }
  199. return;
  200. }
  201. /**
  202. * @access protected
  203. * @param string $host - $host of socket server
  204. * @param string $message - 发送的消息
  205. * @param string $address - 地址
  206. * @return bool
  207. */
  208. protected function send($host, $message = '', $address = '/')
  209. {
  210. $url = 'http://' . $host . ':' . $this->port . $address;
  211. $ch = curl_init();
  212. curl_setopt($ch, CURLOPT_URL, $url);
  213. curl_setopt($ch, CURLOPT_POST, true);
  214. curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
  215. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  216. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
  217. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  218. $headers = [
  219. "Content-Type: application/json;charset=UTF-8",
  220. ];
  221. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
  222. return curl_exec($ch);
  223. }
  224. }