作者 邓超

x

正在显示 1 个修改的文件 包含 24 行增加21 行删除
@@ -33,7 +33,7 @@ class Log { @@ -33,7 +33,7 @@ class Log {
33 */ 33 */
34 public function __construct() 34 public function __construct()
35 { 35 {
36 - $this->setFilename(); 36 +
37 } 37 }
38 38
39 /** 39 /**
@@ -71,12 +71,8 @@ class Log { @@ -71,12 +71,8 @@ class Log {
71 * @author:dc 71 * @author:dc
72 * @time 2023/3/14 11:11 72 * @time 2023/3/14 11:11
73 */ 73 */
74 - private function setFilename($filename=null){  
75 - if($filename){  
76 - $this->filename[md5($filename)] = $filename;  
77 - }else{  
78 - $this->filename['default'] = $filename = LOG_PATH.'/'.app()->nowDate().'.error.log';  
79 - } 74 + private function setFilename($filename){
  75 + $this->filename[md5($filename)] = $filename;
80 76
81 // 创建目录 77 // 创建目录
82 if(!is_dir(dirname($filename))){ 78 if(!is_dir(dirname($filename))){
@@ -86,28 +82,35 @@ class Log { @@ -86,28 +82,35 @@ class Log {
86 } 82 }
87 83
88 /** 84 /**
  85 + * @return array
  86 + */
  87 + public function getFilename($key=null): string
  88 + {
  89 + return $this->filename[$key]??LOG_PATH.'/'.date('Y-m-d').'.error.log';
  90 + }
  91 +
  92 + /**
89 * 写入日志 93 * 写入日志
90 * @author:dc 94 * @author:dc
91 * @time 2023/3/14 10:45 95 * @time 2023/3/14 10:45
92 */ 96 */
93 public function write(){ 97 public function write(){
94 98
95 - foreach ($this->filename as $key=>$fn){  
96 - if(!empty($this->message[$key])){  
97 - $fo = @fopen($fn,'a');  
98 - if($fo){  
99 - foreach ($this->message[$key] as &$log){  
100 - @fwrite($fo,$log.PHP_EOL);  
101 - }  
102 - @fclose($fo);  
103 - }  
104 -  
105 - $log = null;  
106 - unset($log); 99 + foreach ($this->message as $key=>$msg){
107 100
108 - $this->message[$key] = null;  
109 - unset($this->message[$key]); 101 + $fo = @fopen($this->getFilename($key),'a');
  102 + if(!$fo){
  103 + $fo = @fopen($this->getFilename(),'a');
110 } 104 }
  105 + if($fo){
  106 + foreach ($msg as $log){
  107 + @fwrite($fo,$log.PHP_EOL);
  108 + }
  109 + @fclose($fo);
  110 + }
  111 +
  112 + $this->message[$key] = null;
  113 + unset($this->message[$key]);
111 114
112 } 115 }
113 116