2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Defines the English language pack for the base application.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
45 require_once('include/SugarLogger/LoggerManager.php');
46 require_once('include/SugarLogger/LoggerTemplate.php');
49 * Default SugarCRM Logger
52 class SugarLogger implements LoggerTemplate
55 * properties for the SugarLogger
57 protected $logfile = 'sugarcrm';
58 protected $ext = '.log';
59 protected $dateFormat = '%c';
60 protected $logSize = '10MB';
61 protected $maxLogs = 10;
62 protected $filesuffix = "";
63 protected $log_dir = '.';
67 * used for config screen
69 public static $filename_suffix = array(
70 "%m_%Y" => "Month_Year",
71 "%w_%m" => "Week_Month",
72 "%m_%d_%y" => "Month_Day_Year",
76 * Let's us know if we've initialized the logger file
78 protected $initialized = false;
83 protected $fp = false;
85 public function __get(
93 * Used by the diagnostic tools to get SugarLogger log file information
95 public function getLogFileNameWithPath()
97 return $this->full_log_file;
101 * Used by the diagnostic tools to get SugarLogger log file information
103 public function getLogFileName()
105 return ltrim($this->full_log_file, "./");
111 * Reads the config file for logger settings
113 public function __construct()
115 $config = SugarConfig::getInstance();
116 $this->ext = $config->get('logger.file.ext', $this->ext);
117 $this->logfile = $config->get('logger.file.name', $this->logfile);
118 $this->dateFormat = $config->get('logger.file.dateFormat', $this->dateFormat);
119 $this->logSize = $config->get('logger.file.maxSize', $this->logSize);
120 $this->maxLogs = $config->get('logger.file.maxLogs', $this->maxLogs);
121 $this->filesuffix = $config->get('logger.file.suffix', $this->filesuffix);
122 $log_dir = $config->get('log_dir' , $this->log_dir);
123 $this->log_dir = $log_dir . (empty($log_dir)?'':'/');
125 $this->_doInitialization();
126 LoggerManager::setLogger('default','SugarLogger');
130 * Handles the SugarLogger initialization
132 protected function _doInitialization()
134 $this->full_log_file = $this->log_dir . $this->logfile . $this->ext;
135 $this->initialized = $this->_fileCanBeCreatedAndWrittenTo();
140 * Checks to see if the SugarLogger file can be created and written to
142 protected function _fileCanBeCreatedAndWrittenTo()
144 $this->_attemptToCreateIfNecessary();
145 return file_exists($this->full_log_file) && is_writable($this->full_log_file);
149 * Creates the SugarLogger file if it doesn't exist
151 protected function _attemptToCreateIfNecessary()
153 if (file_exists($this->full_log_file)) {
156 @touch($this->full_log_file);
160 * see LoggerTemplate::log()
167 if (!$this->initialized) {
170 //lets get the current user id or default to -none- if it is not set yet
171 $userID = (!empty($GLOBALS['current_user']->id))?$GLOBALS['current_user']->id:'-none-';
173 //if we haven't opened a file pointer yet let's do that
174 if (! $this->fp)$this->fp = fopen ($this->full_log_file , 'a' );
177 // change to a string if there is just one entry
178 if ( is_array($message) && count($message) == 1 )
179 $message = array_shift($message);
180 // change to a human-readable array output if it's any other array
181 if ( is_array($message) )
182 $message = print_r($message,true);
184 //write out to the file including the time in the dateFormat the process id , the user id , and the log level as well as the message
186 strftime($this->dateFormat) . ' [' . getmypid () . '][' . $userID . '][' . strtoupper($level) . '] ' . $message . "\n"
191 * rolls the logger file to start using a new file
193 protected function rollLog(
197 if (!$this->initialized || empty($this->logSize)) {
200 // lets get the number of megs we are allowed to have in the file
201 $megs = substr ( $this->logSize, 0, strlen ( $this->logSize ) - 2 );
202 //convert it to bytes
203 $rollAt = ( int ) $megs * 1024 * 1024;
204 //check if our log file is greater than that or if we are forcing the log to roll
205 if ($force || filesize ( $this->full_log_file ) >= $rollAt) {
206 //now lets move the logs starting at the oldest and going to the newest
207 for($i = $this->maxLogs - 2; $i > 0; $i --) {
208 if (file_exists ( $this->log_dir . $this->logfile . $i . $this->ext )) {
210 $old_name = $this->log_dir . $this->logfile . $i . $this->ext;
211 $new_name = $this->log_dir . $this->logfile . $to . $this->ext;
212 //nsingh- Bug 22548 Win systems fail if new file name already exists. The fix below checks for that.
213 //if/else branch is necessary as suggested by someone on php-doc ( see rename function ).
214 sugar_rename($old_name, $new_name);
216 //rename ( $this->logfile . $i . $this->ext, $this->logfile . $to . $this->ext );
219 //now lets move the current .log file
220 sugar_rename ($this->full_log_file, $this->log_dir . $this->logfile . '1' . $this->ext);
226 * This is needed to prevent unserialize vulnerability
228 public function __wakeup()
230 // clean all properties
231 foreach(get_object_vars($this) as $k => $v) {
234 throw new Exception("Not a serializable object");
240 * Closes the SugarLogger file handle
242 public function __destruct()