<?php<weibo.com/luofei614>declare (strict_types = 1);
namespace think\log\driver;
use Psr\Container\NotFoundExceptionInterface;
use think\App;
use think\contract\LogHandlerInterface;
/**
* github: https://github.com/luofei614/SocketLog
* @author luofei614<weibo.com/luofei614>
*/
class Socket implements LogHandlerInterface
{
protected $app;
protected $config = [
'host' => 'localhost',
'port' => 1116,
'show_included_files' => false,
'force_client_ids' => [],
'allow_client_ids' => [],
'debug' => false,
'expand_level' => ['debug'],
'format_head' => null,
'curl_opt' => [
CURLOPT_CONNECTTIMEOUT => 1,
CURLOPT_TIMEOUT => 10,
],
];
protected $css = [
'sql' => 'color:#009bb4;',
'sql_warn' => 'color:#009bb4;font-size:14px;',
'error' => 'color:#f4006b;font-size:14px;',
'page' => 'color:#40e2ff;background:#171717;',
'big' => 'font-size:20px;color:red;',
];
protected $allowForceClientIds = [];
protected $clientArg = [];
public function __construct(App $app, array $config = [])
{
$this->app = $app;
if (!empty($config)) {
$this->config = array_merge($this->config, $config);
}
if (!isset($config['debug'])) {
$this->config['debug'] = $app->isDebug();
}
}
public function save(array $log = []): bool
{
if (!$this->check()) {
return false;
}
$trace = [];
if ($this->config['debug']) {
if ($this->app->exists('request')) {
$currentUri = $this->app->request->url(true);
} else {
$currentUri = 'cmd:' . implode(' ', $_SERVER['argv'] ?? []);
}
if (!empty($this->config['format_head'])) {
try {
$currentUri = $this->app->invoke($this->config['format_head'], [$currentUri]);
} catch (NotFoundExceptionInterface $notFoundException) {
}
}
$trace[] = [
'type' => 'group',
'msg' => $currentUri,
'css' => $this->css['page'],
];
}
$expandLevel = array_flip($this->config['expand_level']);
foreach ($log as $type => $val) {
$trace[] = [
'type' => isset($expandLevel[$type]) ? 'group' : 'groupCollapsed',
'msg' => '[ ' . $type . ' ]',
'css' => $this->css[$type] ?? '',
];
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$trace[] = [
'type' => 'log',
'msg' => $msg,
'css' => '',
];
}
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
}
if ($this->config['show_included_files']) {
$trace[] = [
'type' => 'groupCollapsed',
'msg' => '[ file ]',
'css' => '',
];
$trace[] = [
'type' => 'log',
'msg' => implode("\n", get_included_files()),
'css' => '',
];
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
}
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
$tabid = $this->getClientArg('tabid');
if (!$clientId = $this->getClientArg('client_id')) {
$clientId = '';
}
if (!empty($this->allowForceClientIds)) {
foreach ($this->allowForceClientIds as $forceClientId) {
$clientId = $forceClientId;
$this->sendToClient($tabid, $clientId, $trace, $forceClientId);
}
} else {
$this->sendToClient($tabid, $clientId, $trace, '');
}
return true;
}
protected function sendToClient($tabid, $clientId, $logs, $forceClientId)
{
$logs = [
'tabid' => $tabid,
'client_id' => $clientId,
'logs' => $logs,
'force_client_id' => $forceClientId,
];
$msg = json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR);
$address = '/' . $clientId;
$this->send($this->config['host'], $this->config['port'], $msg, $address);
}
protected function check()
{
$tabid = $this->getClientArg('tabid');
if (!$tabid && !$this->config['force_client_ids']) {
return false;
}
$allowClientIds = $this->config['allow_client_ids'];
if (!empty($allowClientIds)) {
$this->allowForceClientIds = array_intersect($allowClientIds, $this->config['force_client_ids']);
if (!$tabid && count($this->allowForceClientIds)) {
return true;
}
$clientId = $this->getClientArg('client_id');
if (!in_array($clientId, $allowClientIds)) {
return false;
}
} else {
$this->allowForceClientIds = $this->config['force_client_ids'];
}
return true;
}
protected function getClientArg(string $name)
{
if (!$this->app->exists('request')) {
return '';
}
if (empty($this->clientArg)) {
if (empty($socketLog = $this->app->request->header('socketlog'))) {
if (empty($socketLog = $this->app->request->header('User-Agent'))) {
return '';
}
}
if (!preg_match('/SocketLog\((.*?)\)/', $socketLog, $match)) {
$this->clientArg = ['tabid' => null, 'client_id' => null];
return '';
}
parse_str($match[1] ?? '', $this->clientArg);
}
if (isset($this->clientArg[$name])) {
return $this->clientArg[$name];
}
return '';
}
protected function send($host, $port, $message = '', $address = '/')
{
$url = 'http://' . $host . ':' . $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, $this->config['curl_opt'][CURLOPT_CONNECTTIMEOUT] ?? 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['curl_opt'][CURLOPT_TIMEOUT] ?? 10);
$headers = [
"Content-Type: application/json;charset=UTF-8",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec($ch);
}
}