Alex Trofimov преди 1 година
родител
ревизия
c723d7ce65

+ 26 - 2
inc/classes/BxDolLogs.php

@@ -101,6 +101,26 @@ class BxDolLogs extends BxDolFactory implements iBxDolFactoryObject
         return ($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject] = $o);
     }
 
+    static public function getLevel2NameMap()
+    {
+        return [
+            BX_LOG_ERR => 'ERROR',
+            BX_LOG_WARN => 'WARNING',
+            BX_LOG_INFO => 'INFO',
+            BX_LOG_DEBUG => 'DEBUG',
+        ];
+    }
+
+    static public function getName2LevelsMap()
+    {
+        return [
+            'ERROR' => BX_LOG_ERR,
+            'WARNING' => BX_LOG_WARN | BX_LOG_ERR,
+            'INFO' => BX_LOG_INFO | BX_LOG_WARN | BX_LOG_ERR,
+            'DEBUG' => BX_LOG_DEBUG | BX_LOG_INFO | BX_LOG_WARN | BX_LOG_ERR,
+        ];
+    }
+
     /**
      * Get current logs object name
      */
@@ -114,9 +134,13 @@ class BxDolLogs extends BxDolFactory implements iBxDolFactoryObject
      * @param $mixed string or array to log
      * @return true on success or false on error
      */
-    public function add($mixed)
+    public function add($mixed, $iLevel = BX_LOG_DEBUG)
     {
-        return $this->_oLogsStorage->add($this, $mixed);
+        $a = $this->getName2LevelsMap();
+        $sLogLevel = getParam('sys_logs_level');
+        if (isset($a[$sLogLevel]) && ($a[$sLogLevel] & $iLevel))
+            return $this->_oLogsStorage->add($this, $mixed, $iLevel);
+        return true;
     }
 
     /**

+ 4 - 7
inc/classes/BxDolLogsQuery.php

@@ -21,14 +21,11 @@ class BxDolLogsQuery extends BxDolDb
     static public function getLogsObject($sObject)
     {
         $oDb = BxDolDb::getInstance();
+        $a = $oDb->fromCache('sys_objects_logs', 'getAllWithKey', "SELECT * FROM `sys_objects_logs`", 'object');
+        if ($a && isset($a[$sObject]))
+            return $a[$sObject];
 
-        $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_logs` WHERE `object` = ?", $sObject);
-
-        $aObject = $oDb->getRow($sQuery);
-        if(!$aObject || !is_array($aObject))
-            return false;
-
-        return $aObject;
+        return false;
     }
 }
 

+ 7 - 3
inc/classes/BxDolLogsStorageFolder.php

@@ -43,14 +43,14 @@ class BxDolLogsStorageFolder extends BxDolFactory implements iBxDolSingleton
      * @param $mixed string or array to log
      * @return true on success or false on error
      */
-    public function add($oObject, $mixed)
+    public function add($oObject, $mixed, $iLevel)
     {
         if (!$mixed)
             return true;
 
         $sFile = BX_DIRECTORY_PATH_LOGS . $oObject->getObjectName() . '.log';
 
-        $s = $this->formatLogString($oObject, $mixed);
+        $s = $this->formatLogString($oObject, $mixed, false, $iLevel);
 
         $bNewFile = !file_exists($sFile);
         $bRet = file_put_contents($sFile, $s, FILE_APPEND) ? true : false;
@@ -59,10 +59,14 @@ class BxDolLogsStorageFolder extends BxDolFactory implements iBxDolSingleton
         return $bRet;
     }
 
-    protected function formatLogString($oObject, $mixed, $bIncludeObjectName = false)
+    protected function formatLogString($oObject, $mixed, $bIncludeObjectName = false, $iLevel = BX_LOG_DEBUG)
     {
+        $aLevelNames = BxDolLogs::getLevel2NameMap();
+
         $s = date('M d H:i:s');
 
+        $s .= " [" . $aLevelNames[$iLevel] . "]";
+
         if ($bIncludeObjectName)
             $s .= ' ' . $oObject->getObjectName();
 

+ 2 - 2
inc/classes/BxDolLogsStoragePHPLog.php

@@ -26,12 +26,12 @@ class BxDolLogsStoragePHPLog extends BxDolLogsStorageFolder implements iBxDolSin
      * @param $mixed string or array to log
      * @return true on success or false on error
      */
-    public function add($oObject, $mixed)
+    public function add($oObject, $mixed, $iLevel)
     {
         if (!$mixed)
             return true;
 
-        $s = $this->formatLogString($oObject, $mixed, true);
+        $s = $this->formatLogString($oObject, $mixed, true, $iLevel);
 
         return error_log($s, 0);
     }

+ 2 - 2
inc/classes/BxDolLogsStorageSTDErr.php

@@ -26,12 +26,12 @@ class BxDolLogsStorageSTDErr extends BxDolLogsStorageFolder implements iBxDolSin
      * @param $mixed string or array to log
      * @return true on success or false on error
      */
-    public function add($oObject, $mixed)
+    public function add($oObject, $mixed, $iLevel)
     {
         if (!$mixed)
             return true;
 
-        $s = $this->formatLogString($oObject, $mixed, true);
+        $s = $this->formatLogString($oObject, $mixed, true, $iLevel);
 
         if (!($fd = fopen('php://stderr', 'w')))
             return false;

+ 7 - 2
inc/utils.inc.php

@@ -39,6 +39,11 @@ define('BX_EMAIL_MASS', 2); ///< mass email, one mesage send to manu users, with
 
 define('BX_MAINTENANCE_FILE', '.bx_maintenance'); ///< file name to use as mantenance mode indicator
 
+define('BX_LOG_ERR', 1); ///< error log level format @see bx_log
+define('BX_LOG_WARN', 2); ///< warning log level format @see bx_log
+define('BX_LOG_INFO', 4); ///< info log level format @see bx_log
+define('BX_LOG_DEBUG', 8); ///< debug log level format @see bx_log
+
 /**
  * The following two functions are needed to convert title to uri and back.
  * It usefull when titles are used in URLs, like in Categories and Tags.
@@ -690,7 +695,7 @@ function echoDbg( $what, $desc = '' )
 
 function echoDbgLog($mWhat, $sDesc = 'unused', $sFileName = 'unused')
 {
-    bx_log('sys_debug', $mWhat);
+    bx_log('sys_debug', $mWhat, BX_LOG_DEBUG);
 }
 
 function bx_is_dbg()
@@ -2470,7 +2475,7 @@ function bx_get_device_pixel_ratio()
  * @param $sObject - logs object
  * @param $mixed - string or array to log
  */
-function bx_log($sObject, $mixed)
+function bx_log($sObject, $mixed, $iLevel = BX_LOG_DEBUG)
 {
     if (class_exists('BxDolLogs', true) && $o = BxDolLogs::getObjectInstance($sObject))
         return $o->add($mixed);

+ 1 - 0
install/sql/system.sql

@@ -424,6 +424,7 @@ INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `ex
 (@iCategoryId, 'sys_account_activation_letter', '_adm_stg_cpt_option_sys_account_activation_letter', '', 'checkbox', '', '', '', '', 120),
 
 (@iCategoryId, 'sys_logs_storage_default', '_adm_stg_cpt_option_sys_logs_storage_default', 'Folder', 'select', 'Folder,PHPLog,STDErr', '', '', '', 130),
+(@iCategoryId, 'sys_logs_level', '_adm_stg_cpt_option_sys_logs_level', 'DEBUG', 'select', 'ERROR,WARNING,INFO,DEBUG', '', '', '', 132),
 
 (@iCategoryId, 'sys_default_socket_timeout', '_adm_stg_cpt_option_sys_default_socket_timeout', '30', 'digit', '', '', '', '', 140),
 (@iCategoryId, 'sys_default_curl_timeout', '_adm_stg_cpt_option_sys_default_curl_timeout', '300', 'digit', '', '', '', '', 141),