Tuesday, April 17, 2018

User names in the sugarcrm.log instead of IDs

So normally the sugarCRM log file is filled with lines like this

Mon Apr 16 07:02:12 2018 [15900][412b8e70-febc-11e6-934a-52d504b662cb][FATAL] No Links found for relationship tasks_activities_1_tasks
Mon Apr 16 07:02:12 2018 [15900][420703ed-f2bc-e3f4-f906-566ef6f1e16d][FATAL] No Links found for relationship accounts_accounts_1
Mon Apr 16 07:02:12 2018 [15900][41a5d4a2-c01b-11e7-88b5-52d504b662cb][FATAL] No Links found for relationship cases_cases_1

Usually I am looking for an entry from a particular user so GUIDs are hard for old human eyes to read.  Even with FIND its a pain.  So I wrote this


<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
chdir(dirname(__FILE__));
define('ENTRY_POINT_TYPE', 'api');
require_once('include/entryPoint.php');
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
sugar_die("logUpdate.php is CLI only.");
}
SugarMetric_Manager::getInstance()->setMetricClass('background')->setTransactionName('cron');
if(empty($current_language)) {
$current_language = $sugar_config['default_language'];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
global $current_user;
$current_user = BeanFactory::getBean('Users');
$current_user->getSystemUser();
$sql = "SELECT id, user_name, is_admin, status FROM users";
$result=$GLOBALS['db']->query($sql, true);
if(file_exists('updateLog.sh')) unlink('updateLog.sh');
$fp = fopen('updateLog.sh','w');
while($hash=$GLOBALS['db']->fetchByAssoc($result)) {
if($hash['is_admin']) $hash['user_name'] .= '*';
if($hash['id']!=1) {
fwrite($fp, "sed -i .bak 's/{$hash['id']}/{$hash['user_name']} ({$hash['status']})/g' \"sugarcrm.log\"\n");
}
unlink('sugarcrm.log.bak');
}
fclose($fp);
view raw logUpdate.php hosted with ❤ by GitHub
This writes out a script and, when run, will rewrite the sugarcrm.log file with user_names instead of IDs.

So to use this you need to
  1. Copy this code to the root SugarCRM directory
  2. run it (php -f updateLog.php)
  3. Update the permissions on the file it creates (chmod 777 updateLog.sh)
  4. Run the script (./updateLog.sh)
Of course this only works on Linux or Mac and maybe the Linux Shell in windows, not sure abou thtat.  But anyway I like it and I hope up do to.

This could also be done in real time by overriding include/SugarLoggger/SugarLogger.php and changing the bit of code that write the line to the log file.  I can show that if anyone needs it, just ask.

No comments:

Post a Comment