REAPI/extend/dever/Log.php
2024-09-29 15:43:18 +08:00

95 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}
}