1<?PHP2 3 classlogs{4 Private $_filepath;//file path5 Private $_filename;//file name6 Private $_filehandle;//file Engine7 8 9 Public functionLogs ($dir=NULL,$filename=NULL){Ten One $this->_filepath =Empty($dir) ? ‘‘:$dir; A $this->_filename =Empty($filename) ?Date(' y-m-d ', Time()).‘. Log ':$filename; - - //Create Path the $path=$this->_createpath ($this->_filepath,$this-_filename); - - if(!$this->_isexist ($path)){//If the log directory does not exist - + if(!Empty($this->_filepath)) {//if the path does not exist - + if(!$this->_createdir ($this->_filepath)) {//Create Path A die("Create log directory not successful"); at } - } - - if(!$this->_createlogfile ($path)){//Create a log - die("Create log file is unsuccessful"); - } in } - to + $path=$this->_createpath ($this->_filepath,$this-_filename); - //Create a log file the $this->_filehandle=fopen($path, "A +"); * } $ Panax Notoginseng - Public functionSetlog ($log)//Write Log the { + //Write Log A $str= ""; the if(Is_array($log)){ + foreach($log as $k=$v){ - $str.=$k." : ".$v." \ n "; $ } $}Else{ - $str=$log." \ n "; - } the - Wuyi if(!fwrite($this->_filehandle,$str)){//Write Log the die("Write Log Error"); - } Wu } - About $ Private function_isexist ($path){ - return file_exists($path); - } - A + Private function_createdir ($dir){ the return Is_dir($dir) or ($this->_createdir (dirname($dir)) andmkdir($dir, 0777)); - } $ the the Private function_createlogfile ($path){ the $handle=fopen($path, "W");// the fclose($handle); - return $this->_isexist ($path); in } the the About Private function_createpath ($dir,$filename){ the if(Empty($dir)){ the return $filename; the}Else{ + return $dir." /".$filename; - } the }Bayi the the function__destruct () { - //Close Log - fclose($this-_filehandle); the } the } the?>
PHP Create log record (encapsulated)