Php

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.

Related Post

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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories