1, 'warning' => 2, 'error' => 3, 'strict' => 4 ); protected static $_level = 0; protected static $_maxSeverity = 'notice'; protected static $_currentSeverity = 'notice'; public static function start() { $backtrace = debug_backtrace(); $args = func_get_args(); if ($args) { self::$_currentSeverity = self::SEVERITY_NOTICE; self::_addLog($args); self::_addFileInfo($backtrace); } self::$_level++; } public static function log($args, $severity = null) { $backtrace = debug_backtrace(); self::_compareSeverities($severity); self::_addLog($args); self::_addFileInfo($backtrace); if (self::$_level == 0) { $title = strip_tags(self::$_logs[0]); $className = $backtrace[1]["class"]; $_parts = explode('_', $className); $moduleName = $_parts[1]; self::_release($title, $moduleName); } } public static function stop() { $backtrace = debug_backtrace(); $args = func_get_args(); self::$_level--; if (self::$_level < 0) { self::$_level = 0; } if ($args) { self::$_currentSeverity = self::SEVERITY_NOTICE; self::_addLog($args); self::_addFileInfo($backtrace); } if (self::$_level == 0) { $title = strip_tags(self::$_logs[0]); $className = $backtrace[1]["class"]; $_parts = explode('_', $className); $moduleName = $_parts[1]; self::_release($title, $moduleName); } } protected static function _clear() { self::$_fileInfo = array(); self::$_logs = array(); self::$_maxSeverity = self::SEVERITY_NOTICE; self::$_currentSeverity = self::SEVERITY_NOTICE; self::$_level = 0; } protected static function _release($title, $moduleName) { if (!Mage::getStoreConfig(self::XML_PATH_ENABLE_LOG)) { self::_clear(); return; } $content = ''; foreach (self::$_logs as $_logRow) { $content .= $_logRow . "\n"; } $fileInfo = ''; foreach (self::$_fileInfo as $_fileInfoRow) { $fileInfo .= $_fileInfoRow . "\n"; } $logger = Mage::getModel("aw_lib/log_logger"); $logger ->setTitle($title) ->setSeverity(self::$_maxSeverity) ->setContent($content) ->setModule($moduleName) ->setFileInfo($fileInfo) ->save(); self::_clear(); } protected static function _addLog($args) { $msg = $args; if (is_array($args)) { $msg = call_user_func_array('sprintf', array_values($args)); } self::$_logs[] = '' . self::_getMessageWithTabs($msg) . '' ; } protected static function _addFileInfo($backtrace) { $fileInfo = ' '; if ($backtrace) { $_className = $backtrace[1]["class"]; $_methodName = $backtrace[1]["function"]; $_codeLine = $backtrace[0]["line"]; $fileInfo = $_className . "::" . $_methodName . ":" . $_codeLine; } self::$_fileInfo[] = '' . $fileInfo . '' ; } protected static function _getMessageWithTabs($msg) { $_tabCount = self::$_level; $_tabString = ""; while ($_tabCount > 0) { $_tabString .= "         "; $_tabCount--; } return $_tabString . $msg; } protected static function _compareSeverities($severity = null) { if (!$severity) { $severity = self::SEVERITY_NOTICE; } self::$_currentSeverity = $severity; if (self::$_severitiesPriority[self::$_maxSeverity] < self::$_severitiesPriority[$severity]) { self::$_maxSeverity = $severity; } } }