Last Updated: February 25, 2016
·
797
· antony492

Log What Is Happening

Use this function to easily log what is happening in the code. The file will be put at the filepath and no matter what happens on the webpage, the file will show what ever you send as the message (as long as the code steps past the function call).

 /**
   * @version 3.0
   * @param string $message
   * @param string $name
   * @param boolean $fileAppend
   * @param string $directory
   */
   private function print_log($message, $name="default", $showTimeStamp = true, $fileAppend=true, $directory = __DIR__) {

    $filepath = $directory."\\".$name.".log";
    if($showTimeStamp){
        //Decides whether to create a new file, or append to the existing one.
        if($fileAppend){
            file_put_contents($filepath,"------------------------------------------"."\r\n",FILE_APPEND);

        }else{
            file_put_contents($filepath,"------------------------------------------"."\r\n",);

        }

        file_put_contents($filepath,date("Y-m-d H:i:s")."\r\n",FILE_APPEND);
        file_put_contents($filepath,"------------------------------------------"."\r\n",FILE_APPEND);
    }

    //If this is true, then the file would have been created above and therefore message would need to be appended.
    if($showTimeStamp){
        file_put_contents($filepath,$message."\r\n",FILE_APPEND);

    }else{
        file_put_contents($filepath.$name.".log",$message."\r\n");

    }

}