SugarCRM:添加自定义助手类
最近我进入了SugarCRM,ATM,我必须实现定制的记录器,其想法是在保存逻辑钩子后从Bean对象中获取一些相关数据,并根据请求将其放入XML中。 我认为将其作为一个Sugar模块来实现可能是错误的方法 问题是把我的类放在目录层次结构的什么地方, 我该如何引导他SugarCRM:添加自定义助手类,sugarcrm,suitecrm,Sugarcrm,Suitecrm,最近我进入了SugarCRM,ATM,我必须实现定制的记录器,其想法是在保存逻辑钩子后从Bean对象中获取一些相关数据,并根据请求将其放入XML中。 我认为将其作为一个Sugar模块来实现可能是错误的方法 问题是把我的类放在目录层次结构的什么地方, 我该如何引导他 提前感谢您希望将您的类放在custom/include/SugarLogger中,并将其命名为SugarXMLLogger。这是灵活的,但遵循SugarCRM中的约定。确保将类命名为与文件相同的名称 您至少应该实现LoggerTemp
提前感谢您希望将您的类放在custom/include/SugarLogger中,并将其命名为SugarXMLLogger。这是灵活的,但遵循SugarCRM中的约定。确保将类命名为与文件相同的名称 您至少应该实现LoggerTemplate,如果您想要SugarCRM使用的默认记录器的完整结构,您应该扩展SugarLogger。然而,对于一个简单的XML记录器来说,这并不是完全必要的 虽然我知道您没有要求代码来实际执行日志记录,但在测试定制日志记录程序的实际构建时,我决定创建一个。这是我尝试使用SimpleXML创建一个非常简单的XML记录器。我对pingapi进行了测试,以观察它使用致命日志到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;
}
}
@MatthewPoer可能重复,请阅读问题,我对如何实现Logger没有问题,我对我的代码应该放在哪里有问题。干杯,Vovate那篇文章的要点是custom/Company/Helper/Logger.php,这对我来说似乎很奇怪,我会在custom/include/MyLogger.php创建类文件,以便在逻辑钩子中轻松找到、需要和调用它,下面是我得到的答案:因为您可以在logic_hooks.php中指定要执行的内容,所以您可以将自己的代码放在任何您想要的地方。就我个人而言,我将几个模块使用的代码放在sugarCRM/custom/mystuff/目录中。我使用…/modules/hooks/目录查找特定于该模块的内容。