SugarCRM:添加自定义帮助类
问题描述:
最近进入SugarCRM,ATM我必须实现自定义Logger,其想法是从保存逻辑钩子并将其放入XML(根据请求)后从Bean对象获取一些相关数据。 我认为把它作为一个Sugar模块来实现可能是错误的方法。SugarCRM:添加自定义帮助类
问题是我们的课程在目录层次结构 中的权利以及我应该如何引导他?
在此先感谢
答
你会希望把你的类定制/包括/ 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糖的文件?(
可能重复http://*.com/questions/26143788/can-we-create-custom-logs-files- in-sugar-crm-through-its-own-methods) – 2014-10-07 11:34:59
@MatthewPoer,请阅读这个问题,我对如何实现Logger没有任何问题,我有问题应该在哪里放置我的代码。干杯,Vova – 2014-10-07 12:12:46
在这篇文章中的要点建议'custom/Company/Helper/Logger.php'这对我来说似乎很奇怪,我会在'custom/include/MyLogger.php'创建类文件,以便它很容易在逻辑钩子中找到,需要和调用。 – 2014-10-07 12:19:14