Tuesday 31 December 2019

How to create and manage log file in php?


Create and manage crud operation log data PHP

<?php
function crud_log($action = '',$message = '',$array_data = array()){
    global $handle_crud_log;
    date_default_timezone_set("Australia/Sydney"); //Ex. set australia/sydney timezone   
    $crud_log_dir = "crud-log"; //Create this DIR(crud-log) on file dir location
   
    if (!file_exists($crud_log_dir)){
        // create directory/folder uploads.
        mkdir($crud_log_dir, 755, true);
    }   

    $quote_log_file_name =  "/crud_log_".date("m_Y").".log";
    $ip_address = @$_SERVER['REMOTE_ADDR']." #### ". @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $date_time = date("d/m/Y H:i:s");
    $action_name = $action; // (Ex. Create/Read/Update/Delete)
    $crud_msg = $message; //Create, Read, Update and Delete message;
    $array_data = $array_data; //Array data type Like $_POST
       
    $handle_crud_log = fopen($crud_log_dir.$quote_log_file_name, "a");
    fwrite($handle_crud_log, "=================================================\r\n");   
    fwrite($handle_crud_log, "At: {$date_time}\r\n");
    fwrite($handle_crud_log, "From: {$ip_address}\r\n");
    fwrite($handle_crud_log, "Action: {$action_name} \r\n");
    fwrite($handle_crud_log, "Message: {$crud_msg}\r\n");
    fwrite($handle_crud_log, "Data: ". print_r($array_data, true));
    fwrite($handle_crud_log, "=================================================\r\n\r\n");
}
$action = "Update";
$message = "Update emplyee detail";
$array_data = array("f_name"=>"PHP", "l_name"=>"Kishan");
crud_log($action, $message, $array_data); // call function for create log
?>

2 comments:

  1. Website Speed Optimization is using the strategies and techniques to speed up your site & make it faster to load in the web browsers. If your website loads slowly then you will lose your site rankings in search engine results. It requires a lot of efforts & tweaks to optimize your website speed. I have listed over 5 website speed optimization tips to increase your website speed. Moreover, you can use these techniques for both WordPress & Non-WordPress.

    ReplyDelete
  2. useful post, keep uploading!

    ReplyDelete