2

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

问题是将我的类放在目录层次结构中的正确位置,以及我应该如何引导他?

提前致谢

4

1 回答 1

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;
    }
}
于 2014-10-08T19:00:17.543 回答