作者 邓超

x

正在显示 1 个修改的文件 包含 24 行增加21 行删除
... ... @@ -33,7 +33,7 @@ class Log {
*/
public function __construct()
{
$this->setFilename();
}
/**
... ... @@ -71,12 +71,8 @@ class Log {
* @author:dc
* @time 2023/3/14 11:11
*/
private function setFilename($filename=null){
if($filename){
$this->filename[md5($filename)] = $filename;
}else{
$this->filename['default'] = $filename = LOG_PATH.'/'.app()->nowDate().'.error.log';
}
private function setFilename($filename){
$this->filename[md5($filename)] = $filename;
// 创建目录
if(!is_dir(dirname($filename))){
... ... @@ -86,28 +82,35 @@ class Log {
}
/**
* @return array
*/
public function getFilename($key=null): string
{
return $this->filename[$key]??LOG_PATH.'/'.date('Y-m-d').'.error.log';
}
/**
* 写入日志
* @author:dc
* @time 2023/3/14 10:45
*/
public function write(){
foreach ($this->filename as $key=>$fn){
if(!empty($this->message[$key])){
$fo = @fopen($fn,'a');
if($fo){
foreach ($this->message[$key] as &$log){
@fwrite($fo,$log.PHP_EOL);
}
@fclose($fo);
}
$log = null;
unset($log);
foreach ($this->message as $key=>$msg){
$this->message[$key] = null;
unset($this->message[$key]);
$fo = @fopen($this->getFilename($key),'a');
if(!$fo){
$fo = @fopen($this->getFilename(),'a');
}
if($fo){
foreach ($msg as $log){
@fwrite($fo,$log.PHP_EOL);
}
@fclose($fo);
}
$this->message[$key] = null;
unset($this->message[$key]);
}
... ...