Create Logfile and Write Error Log in PHP

in this tutorial, I’ll discuss How to create php Log File using PHP. This quick tutorial help to create logging functionality for your PHP application.

I am using core PHP to create a file and log them. You don’t need to use any third-party PHP library or any plugin.

Creating the Log File Using PHP

There is a general requirement of any PHP application, how to log the info, Error, Exception etc information into your application.

Your application is running fine and accidentally it has been down due to any reason.

How to find the issue that happened? and what problem occurred in your code? Due to the log file you can get the root cause information of the fail system.

The application log file helps to find out information about your PHP application.

Creating logging functionality in php application is very easy. You just need to create a single log.php file into your application. You need to import this file and use it anywhere in the application.

Implement the Log File Using PHP

Let’s add the below code into the log.php file.

//define function name  
function m_log($arMsg)  
{  
	//define empty string                                 
	$stEntry="";  
	//get the event occur date time,when it will happened  
	$arLogData['event_datetime']='['.date('D Y-m-d h:i:s A').'] [client '.$_SERVER['REMOTE_ADDR'].']';  
	//if message is array type  
	if(is_array($arMsg))  
	{  
	//concatenate msg with datetime  
	foreach($arMsg as $msg)  
	$stEntry.=$arLogData['event_datetime']." ".$msg."rn";  
}  
else  
{   //concatenate msg with datetime  
	
	$stEntry.=$arLogData['event_datetime']." ".$arMsg."rn";  
}  
//create file with current date name  
$stCurLogFileName='log_'.date('Ymd').'.txt';  
//open the file append mode,dats the log file will create day wise  
$fHandler=fopen(LOG_PATH.$stCurLogFileName,'a+');  
//write the info into the file  
fwrite($fHandler,$stEntry);  
//close handler  
fclose($fHandler);  
}

How To Use PHP Log Function in Your Application

You can now utilize the m_log() function throughout your application to log relevant information. It’s as simple as making a function call and passing the necessary parameters.

//how to call function: 
m_log("Sorry,returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring);

PHP 7 Exceptions Changes

The PHP does not handle fatal errors gracefully since they immediately terminate program execution. PHP 7 introduced a “Throwable” interface. They provide the ability to catch PHP internal exceptions, like TypeError, ParseError, etc.

try {

// Your code here

} catch(Throwable $ex) {

// You can catch any user or internal PHP exceptions

}

Conclusion

By following the above steps allows you to seamless integration of logging functionality into your PHP applications. This helps to create a dependable system for tracking errors, exceptions, and critical events, streamlining troubleshooting and maintenance processes.

Leave a Reply

Your email address will not be published. Required fields are marked *