SugarCRM:添加自定义助手类

时间:2014-10-07 07:08:16

标签: sugarcrm suitecrm

最近进入SugarCRM,ATM我必须实现自定义Logger,其想法是从保存逻辑钩子之后从Bean对象获取一些相关数据并将其放入XML(根据要求)。 我认为将它作为Sugar模块实现可能是错误的方法。

问题在于它将我的类放在目录层次结构中的权利, 我应该如何引导他?

提前致谢

1 个答案:

答案 0 :(得分:4)

您需要将您的类放在custom / include / 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;
    }
}
相关问题