实现后台及前台通过API访问UV埋点,所有代码全部保存
This commit is contained in:
@@ -8,8 +8,10 @@ error_reporting(E_NOTICE);
|
||||
use extend\QRcode as QRcode;
|
||||
use think\facade\Session;
|
||||
use think\facade\Event;
|
||||
use think\facade\App;
|
||||
use app\model\system\Addon;
|
||||
use extend\Barcode;
|
||||
use app\common\library\CallerInfo;
|
||||
|
||||
/*****************************************************基础函数*********************************************************/
|
||||
|
||||
@@ -1877,6 +1879,77 @@ function paramFilter($param)
|
||||
return preg_replace($filter_rule, '', $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单日志写入
|
||||
* @param string $msg 日志内容
|
||||
* @param string $level 日志级别 debug、info、warning、error
|
||||
* @param string $file 日志文件名(不含路径)
|
||||
*/
|
||||
function log_write(string $message, string $level = 'info', string $filename = '', int $maxFileSize = 5242880): void
|
||||
{
|
||||
$callerInfo = CallerInfo::getCallerInfo(1);
|
||||
|
||||
// 格式化日志内容
|
||||
$content = sprintf(
|
||||
"[%s] %s: %s (文件:%s,行:%d) " . PHP_EOL,
|
||||
date('Y-m-d H:i:s'),
|
||||
strtoupper($level),
|
||||
is_array($message) ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message,
|
||||
$callerInfo['file'],
|
||||
$callerInfo['line']
|
||||
);
|
||||
|
||||
$logPath = app()->getRuntimePath() . 'log/app_run_info/'; // eg. /runtime/shop/log
|
||||
|
||||
// 确保日志目录存在
|
||||
if (!is_dir($logPath)) {
|
||||
mkdir($logPath, 0755, true);
|
||||
}
|
||||
|
||||
// 生成日志文件名
|
||||
if (empty($filename)) {
|
||||
$filename = date('Y-m-d') . '.log';
|
||||
}
|
||||
|
||||
// 获取基础日志文件名(不含扩展名)和扩展名
|
||||
$fileInfo = pathinfo($filename);
|
||||
$baseName = $fileInfo['filename'];
|
||||
$extension = isset($fileInfo['extension']) ? '.' . $fileInfo['extension'] : '.log';
|
||||
|
||||
// 日志文件路径
|
||||
$logFile = $logPath . $filename;
|
||||
|
||||
// 检查文件大小并处理日志分割
|
||||
if (file_exists($logFile) && filesize($logFile) >= $maxFileSize) {
|
||||
// 查找现有的带序号的日志文件,确定下一个序号
|
||||
$nextIndex = 1;
|
||||
$pattern = $logPath . $baseName . '-???' . $extension;
|
||||
$matchingFiles = glob($pattern);
|
||||
|
||||
if (!empty($matchingFiles)) {
|
||||
// 提取最大序号
|
||||
$maxIndex = 0;
|
||||
foreach ($matchingFiles as $file) {
|
||||
$fileBase = pathinfo($file, PATHINFO_FILENAME);
|
||||
if (preg_match('/-([0-9]{3})$/', $fileBase, $matches)) {
|
||||
$index = (int)$matches[1];
|
||||
$maxIndex = max($maxIndex, $index);
|
||||
}
|
||||
}
|
||||
$nextIndex = $maxIndex + 1;
|
||||
}
|
||||
|
||||
// 生成带序号的新文件名
|
||||
$newFilename = $baseName . '-' . sprintf('%03d', $nextIndex) . $extension;
|
||||
$logFile = $logPath . $newFilename;
|
||||
}
|
||||
|
||||
// 写入文件(追加模式)
|
||||
file_put_contents($logFile, $content, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将异常格式化HTML输出,方便定位
|
||||
* @param mixed $th
|
||||
|
||||
Reference in New Issue
Block a user