95 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace dever;
use think\Log as Tool;
class Log
{
private static $size = 20485760;
public static function write($key, $name, $content)
{
if (is_array($content)) {
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
}
$path = self::path($key);
// var_dump($path);die;
$file = $path . $name . '.log';
$exists = false;
if (file_exists($file)) {
$exists = true;
}
if ($exists && self::$size <= filesize($file)) {
rename($file, $file . '.' . date('H_i_s') . '.bak');
$exists = false;
}
$now = date('Y-m-d H:i:s');
$log = $now . ' ~ ' . $content . "\r\n";
// if(!$exists){
// $files = self::mkdirFile($file);
// if($files){
// @chmod($file, 0755);
// @shell_exec('chmod -R 777 ' . $file);
//// @system('chmod -R 777 ' . $file);
// }
// #Call to undefined function dever\system()
//
//
// }
// var_dump($file);die;
$state = @error_log($log, 3, $file);
if (!$exists) {
@chmod($file, 0755);
@shell_exec('chmod -R 777 ' . $file);
// @system('chmod -R 777 ' . $file);
}
return $state;
}
public static function mkdirFile($key)
{
return touch($key);
}
public static function path($key)
{
# 不能写入到public里不然有被查看的风险
$path = root_path();
$path = $path . "extend/" . 'data/logs' . DIRECTORY_SEPARATOR;
// var_dump($path);die;
//return $path;
self::createPath($path);
if ($key) {
$path .= $key . DIRECTORY_SEPARATOR;
}
#xxx/01/02/03
self::createPath($path);
$time = date('Y_m_d');
$date = explode('_', $time);
foreach($date as $k => $v) {
$path .= $v . DIRECTORY_SEPARATOR;
self::createPath($path);
}
return $path;
}
private static function createPath($path)
{
if (!is_dir($path)) {
mkdir($path,0777, true);
@chmod($path, 0755);
@shell_exec('chmod -R 777 ' . $path);
// @system('chmod -R 777 ' . $path);
}
}
}