From ff129750296c8494fd1604dcc7936d1429b3a723 Mon Sep 17 00:00:00 2001 From: appu <apparao@vtiger.com> Date: Tue, 14 Nov 2023 10:26:09 +0530 Subject: [PATCH] Feature::Vtiger Logger enabled using MonoLog library --- modules/Vtiger/helpers/Logger.php | 37 ++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/modules/Vtiger/helpers/Logger.php b/modules/Vtiger/helpers/Logger.php index f2fd2b0e5..32a1686f8 100644 --- a/modules/Vtiger/helpers/Logger.php +++ b/modules/Vtiger/helpers/Logger.php @@ -9,33 +9,51 @@ * All Rights Reserved. * ********************************************************************************** */ +// Include the Monolog library for logging functionality require 'vendor/autoload.php'; +// Import necessary classes from Monolog use Monolog\Logger as MonologLogger; use Monolog\Handler\StreamHandler; +// Define a custom Logger class class Logger { private static $logLevel; private static $filePath; private static $initialized = false; + /** + * Constructor to initialize the logger + * @global type $PERFORMANCE_CONFIG + */ public function __construct() { + // Check if the logger is not already initialized if (!self::$initialized) { global $PERFORMANCE_CONFIG; + // Check if the performance config is set and debug logging is enabled if (isset($PERFORMANCE_CONFIG) && isset($PERFORMANCE_CONFIG['LOGLEVEl_DEBUG']) && $PERFORMANCE_CONFIG['LOGLEVEl_DEBUG']) { - self::$logLevel = 100; // Default log file path.; + // Set the default log level to 100 and the log file path + self::$logLevel = 100; self::$filePath = "logs/vtigercrm.log"; self::$initialized = true; } } } + /** + * Static method to get a logger instance + */ public static function getLogger(string $channel, $customFormatter = true) { + // Create a new logger instance $log = new self(); + + // Check if log level is set (logger is initialized) if (self::$logLevel) { $log = new MonologLogger($channel); $handler = new StreamHandler(self::$filePath, self::$logLevel); + + // Set a custom formatter if customFormatter is true if ($customFormatter) { $handler->setFormatter(new VtigerCustomFormatter()); } @@ -44,25 +62,38 @@ class Logger { return $log; } + // Placeholder method for logging information public function info($message) { - + // Logging info not implemented } + // Placeholder method for logging debug messages public function debug($message) { - + // Logging debug not implemented } } +// Define a custom log formatter use Monolog\Formatter\FormatterInterface; class VtigerCustomFormatter implements FormatterInterface { + /** + * Format a log record + * @param Monolog\LogRecord $record + * @return string + */ public function format(Monolog\LogRecord $record) { $record = $record->toArray(); $formatted = '[' . date('Y-m-d H:i:s') . '] - ' . $record['level_name'] . ' - ' . $record['channel'] . ' - ' . $record['message'] . PHP_EOL; return $formatted; } + /** + * Format a batch of log records + * @param array $records + * @return type string + */ public function formatBatch(array $records) { $formatted = ''; foreach ($records as $record) { -- GitLab