Paka Information

How to create a logfile in PHP

804 0

How to create a logfile in PHP

Today, We want to share with you How to create a logfile in PHP.
In this post we will show you how to php write to log file append, hear for Write a string to a file using PHP we will give you demo and example for implement.
In this post, we will learn about how to create a logfile in php? with an example.

How to create a logfile in PHP Example

How to create a logfile in PHP Example


Hear is simple Source code for create new Date wise log file using php

Creating write a log in php web/API or web – application is very easy.You simple just need to make a write logs error or success message single log.php file use it any place or any where in web-application all the error msg write do you need.

Also Read :  Creating Dynamic XML Sitemaps Generator Using PHP MySQLi

$time_dt_log = date('Y-m-d h:i:sa');
$msg_log = "Your Are Most Welcome in live24u.com";
  
write_logs("**************###### Start Date Log For Day : '" . $time_dt_log . "'###**********");
write_logs($msg_log);
write_logs("**************##### END Date Log For Day : '" . $time_dt_log . "'###**********");
 
function write_logs($msg_log)
{
    $all_log_filesavename = "log";
    if (!file_exists($all_log_filesavename)) 
    {
        // make some directory/folder uploads.
        mkdir($all_log_filesavename, 0777, true);
    }
    $log_file_data = $all_log_filesavename.'/log_' . date('d-M-Y') . '.log';
    file_put_contents($log_file_data, $msg_log . "\n", FILE_APPEND);
}

Example 2 : Write a string to a file using PHP

How to creating log file in you web-application or PHP project in easy way
PHP Source code to log some any errors and Write warnings into a file in php

//define PHP function name  
function live_log($arraymsg)  
{  
	//define empty string                                 
	$stmntEntry="";  
	
	//get the Data event occur PHP date time,when it will happened  
	$Data_arLog['event_datetime']='['.date('D Y-m-d h:i:s A').'] [client '.$_SERVER['REMOTE_ADDR'].']';  
	
	//if comments is array type  
	if(is_array($arraymsg))  
	{  
		//Two String concatenate message with datetime  
	foreach($arraymsg as $message)  
	$stmntEntry.=$Data_arLog['event_datetime']." ".$message."rn";  
}  
else  
{   //concatenate message with datetime  
	
	$stmntEntry.=$Data_arLog['event_datetime']." ".$arraymsg."rn";  
}  
//make file with current date name  
$stCurLogFileName='log_'.date('Ymd').'.txt'; 
 
//open the file append mode,dats the log file will make day wise  
$fileHandle=fopen(LOG_PATH.$stCurLogFileName,'a+');  

//write the info into the file  
fwrite($fileHandle,$stmntEntry);  


//close handler  
fclose($fileHandle);  
}  

how to call use PHP text-file log function in your web-application

//Write a string to a file using PHP
live_log("Sorry,You following ERROR: ".$fault->faultcode."-".$fault->faultstring); 

We hope you get an idea about Write a string to a file using PHP
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

Also Read :  How to Select VueJs Check All Uncheck All Checkboxes

We hope This Post can help you…….Good Luck!.

Leave A Reply

Your email address will not be published.