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.
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.
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); }
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);
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 }
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.
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More