From 1491a7e914c258a8d50d4b2a20ec4c17ffe5ff5a Mon Sep 17 00:00:00 2001
From: prasad <prasad@vtiger.com>
Date: Sat, 4 May 2024 21:09:28 +0530
Subject: [PATCH] Added config_override for development usage and improved log
 handler

---
 config_override.dev.php              |  5 ++++
 vtlib/Vtiger/Utils/PhpLogHandler.php | 38 +++++++++++++++-------------
 2 files changed, 25 insertions(+), 18 deletions(-)
 create mode 100644 config_override.dev.php

diff --git a/config_override.dev.php b/config_override.dev.php
new file mode 100644
index 000000000..72a8ae5f7
--- /dev/null
+++ b/config_override.dev.php
@@ -0,0 +1,5 @@
+<?php
+
+/** Logging configuration for Strict Development */
+require_once "vtlib/Vtiger/Utils/PhpLogHandler.php";
+Vtiger_PhpLogHandler::enableStrictLogging(__DIR__, "logs/phperr.log");
diff --git a/vtlib/Vtiger/Utils/PhpLogHandler.php b/vtlib/Vtiger/Utils/PhpLogHandler.php
index cfad35f71..5824bd74a 100644
--- a/vtlib/Vtiger/Utils/PhpLogHandler.php
+++ b/vtlib/Vtiger/Utils/PhpLogHandler.php
@@ -8,20 +8,20 @@
  * All Rights Reserved.
  *************************************************************************************/
 
-/**
- * USAGE: For E_ALL strict development - create (config_override.php) with code below.
- * 
- * ini_set("display_errors", "off");
- * error_reporting(E_ALL);
- * ini_set("log_errors", "on");
- * ini_set("error_log", "logs/phperr.log");
- * require_once "vtlib/Vtiger/Utils/PhpLogHandler.php";
- * set_error_handler(Vtiger_PhpLogHandler::getErrorHandler(__DIR__));
- * set_exception_handler(Vtiger_PhpLogHandler::getExceptionHandler(__DIR__));
- */
-
 class Vtiger_PhpLogHandler {
 
+    /**
+     * Enable strict mode.
+     */
+    public static function enableStrictLogging($basedir, $logfile) {
+        ini_set("display_errors", "off");
+        error_reporting(E_ALL);
+        ini_set("log_errors", "on");
+        ini_set("error_log", $logfile);
+        set_error_handler(Vtiger_PhpLogHandler::getErrorHandler($basedir));
+        set_exception_handler(Vtiger_PhpLogHandler::getExceptionHandler($basedir));
+    }
+
     /**
      * Capture context of request in Log to review later.
      */
@@ -30,7 +30,7 @@ class Vtiger_PhpLogHandler {
         if (isset($_SERVER)) {
             $ctx = $_SERVER["REQUEST_METHOD"] . " " . str_replace("?" . $_SERVER["QUERY_STRING"], "", $_SERVER["REQUEST_URI"]);
             $params = [];
-            foreach (["module", "view", "action", "mode"] as $key) {
+            foreach (["module", "view", "action", "mode", "record"] as $key) {
                 if (isset($_REQUEST[$key])) $params[$key] = $_REQUEST[$key];
             }
             $ctx .= "?" . http_build_query($params);
@@ -49,8 +49,9 @@ class Vtiger_PhpLogHandler {
         if (!$logfile) $logfile = ini_get('error_log');
 
         $logctx = Vtiger_PhpLogHandler::getRequestContextToLog();
+        $reqtm  = date("[Y-m-d H:i:s]");
 
-        return function($errno, $errstr, $errfile, $errline) use ($display_errors, $log_errors, $basedir, $logfile, $logctx) {
+        return function($errno, $errstr, $errfile, $errline) use ($display_errors, $log_errors, $basedir, $logfile, $logctx, $reqtm) {
             // nothing todo return early.
             if (!$display_errors && !$log_errors && ($log_errors && !$logfile)) {
                 return;
@@ -79,7 +80,7 @@ class Vtiger_PhpLogHandler {
             // format message same as default PHP
             $msg = sprintf("%s: %s in %s on line %s\n", $errtype, $errstr, $errfilerel, $errline);
             if ($logfile) {
-                $tmstamp = date("[Y-m-d H:i:s]");
+                $tmstamp = $reqtm . "@". date("[H:i:s]");
                 $fullmsg = $tmstamp . " ". $logctx . "\n" . $tmstamp . " " . $msg;
                 file_put_contents($logfile, $fullmsg, FILE_APPEND | LOCK_EX);
             }
@@ -103,8 +104,9 @@ class Vtiger_PhpLogHandler {
         if (!$logfile) $logfile = ini_get('error_log');
 
         $logctx = Vtiger_PhpLogHandler::getRequestContextToLog();
+        $reqtm  = date("[Y-m-d H:i:s]");
 
-        return function(Throwable $e) use ($display_errors, $log_errors, $basedir, $logfile, $logctx) {
+        return function(Throwable $e) use ($display_errors, $log_errors, $basedir, $logfile, $logctx, $reqtm) {
             // nothing todo return early.
             if (!$display_errors && !$log_errors && ($log_errors && !$logfile)) {
                 return;
@@ -117,7 +119,7 @@ class Vtiger_PhpLogHandler {
             // format message same as default PHP
             $msg = sprintf("Fatal error: %s in %s:%d\nStack trace:\n%s\n    thrown in %s on line %d\n", $e->getMessage(), $errfile, $e->getLine(), $errstack, $errfile, $e->getLine());
             if ($logfile) {
-                $tmstamp = date("[Y-m-d H:i:s]");
+                $tmstamp = $reqtm . "@" . date("[H:i:s]");
                 $fullmsg = $tmstamp . " ". $logctx . "\n" . $tmstamp . " " . $msg;
                 file_put_contents($logfile, $fullmsg, FILE_APPEND | LOCK_EX);
             } 
@@ -130,4 +132,4 @@ class Vtiger_PhpLogHandler {
         };
     }
 
-}
\ No newline at end of file
+}
-- 
GitLab