2014-10-07 49 views
2

最近進入SugarCRM,ATM我必須實現自定義Logger,其想法是從保存邏輯鉤子並將其放入XML(根據請求)後從Bean對象獲取一些相關數據。 我認爲把它作爲一個Sugar模塊來實現可能是錯誤的方法。SugarCRM:添加自定義幫助類

問題是我們的課程在目錄層次結構 中的權利以及我應該如何引導他?

在此先感謝

+0

可能重複http://stackoverflow.com/questions/26143788/can-we-create-custom-logs-files- in-sugar-crm-through-its-own-methods) – 2014-10-07 11:34:59

+0

@MatthewPoer,請閱讀這個問題,我對如何實現Logger沒有任何問題,我有問題應該在哪裏放置我的代碼。乾杯,Vova – 2014-10-07 12:12:46

+1

在這篇文章中的要點建議'custom/Company/Helper/Logger.php'這對我來說似乎很奇怪,我會在'custom/include/MyLogger.php'創建類文件,以便它很容易在邏輯鉤子中找到,需要和調用。 – 2014-10-07 12:19:14

回答

4

你會希望把你的類定製/包括/ SugarLogger並將其命名爲類似SugarXMLLogger(這是靈活的,但內SugarCRM公司如下約定)。確保將文件命名爲與文件相同的類。

你應該至少實現LoggerTemplate,如果你想要SugarCRM使用的默認記錄器的完整結構,你可能想要擴展SugarLogger。但是,對於簡單的XML記錄器來說,這不是完全必要的。

雖然我知道你並沒有要求代碼來實際執行日誌記錄,但在測試自定義記錄器的實際構建時,我決定創建一個。這是我使用SimpleXML的一個非常簡單的XML記錄器的嘗試。我對Ping API進行了測試,觀察它使用致命日誌到XML和所有日誌到XML。

<?php 
/** 
* Save to custom/include/SugarLogger/SugarXMLLogger.php 
* 
* Usage: 
* To make one particular log level write to the XML log do this: 
* ```php 
* <?php 
* LoggerManager::setLogger('fatal', 'SugarXMLLogger'); 
* $GLOBALS['log']->fatal('Testing out the XML logger'); 
* ``` 
* 
* To make all levels log to the XML log, do this 
* ```php 
* <?php 
* LoggerManager::setLogger('default', 'SugarXMLLogger'); 
* $GLOBALS['log']->warn('Testing out the XML logger'); 
* ``` 
*/ 

/** 
* Get the interface that his logger should implement 
*/ 
require_once 'include/SugarLogger/LoggerTemplate.php'; 

/** 
* SugarXMLLogger - A very simple logger that will save log entries into an XML 
* log file 
*/ 
class SugarXMLLogger implements LoggerTemplate 
{ 
    /** 
    * The name of the log file 
    * 
    * @var string 
    */ 
    protected $logfile = 'sugarcrm.log.xml'; 

    /** 
    * The format for the timestamp entry of the log 
    * 
    * @var string 
    */ 
    protected $dateFormat = '%c'; 

    /** 
    * The current SimpleXMLElement logger resource 
    * 
    * @var SimpleXMLElement 
    */ 
    protected $currentData; 

    /** 
    * Logs an entry to the XML log 
    * 
    * @param string $level The log level being logged 
    * @param array $message The message to log 
    * @return boolean True if the log was saved 
    */ 
    public function log($level, $message) 
    { 
     // Get the current log XML 
     $this->setCurrentLog(); 

     // Append to it 
     $this->appendToLog($level, $message); 

     // Save it 
     return $this->saveLog(); 
    } 

    /** 
    * Saves the log file 
    * 
    * @return boolean True if the save was successful 
    */ 
    protected function saveLog() 
    { 
     $write = $this->currentData->asXML(); 
     return sugar_file_put_contents_atomic($this->logfile, $write); 
    } 

    /** 
    * Sets the SimpleXMLElement log object 
    * 
    * If there is an existing log, it will consume it. Otherwise it will create 
    * a SimpleXMLElement object from a default construct. 
    */ 
    protected function setCurrentLog() 
    { 
     if (file_exists($this->logfile)) { 
      $this->currentData = simplexml_load_file($this->logfile); 
     } else { 
      sugar_touch($this->logfile); 
      $this->currentData = simplexml_load_string("<?xml version='1.0' standalone='yes'?><entries></entries>"); 
     } 
    } 

    /** 
    * Adds an entry of level $level to the log, with message $message 
    * 
    * @param string $level The log level being logged 
    * @param array $message The message to log 
    */ 
    protected function appendToLog($level, $message) 
    { 
     // Set some basics needed for every entry, starting with the current 
     // user id 
     $userID = $this->getUserID(); 

     // Get the process id 
     $pid = getmypid(); 

     // Get the message to log 
     $message = $this->getMessage($message); 

     // Set the timestamp 
     $timestamp = strftime($this->dateFormat); 

     // Add it to the data now 
     $newEntry = $this->currentData->addChild('entry'); 
     $newEntry->addChild('timestamp', $timestamp); 
     $newEntry->addChild('pid', $pid); 
     $newEntry->addChild('userid', $userID); 
     $newEntry->addChild('level', $level); 
     $newEntry->addChild('message', $message); 
    } 

    /** 
    * Gets the user id for the current user, or '-none-' if the current user ID 
    * is not attainable 
    * 
    * @return string The ID of the current user 
    */ 
    protected function getUserID() 
    { 
     if (!empty($GLOBALS['current_user']->id)) { 
      return $GLOBALS['current_user']->id; 
     } 

     return '-none-'; 
    } 

    /** 
    * Gets the message in a loggable format 
    * 
    * @param mixed $message The message to log, as a string or an array 
    * @return string The message to log, as a string 
    */ 
    protected function getMessage($message) 
    { 
     if (is_array($message) && count($message) == 1) { 
      $message = array_shift($message); 
     } 

     // change to a human-readable array output if it's any other array 
     if (is_array($message)) { 
      $message = print_r($message,true); 
     } 

     return $message; 
    } 
} 
的[我們可以創建自定義的通過自己的方式登錄CRM糖的文件?(