]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarLogger/SugarLogger.php
Release 6.2.0
[Github/sugarcrm.git] / include / SugarLogger / SugarLogger.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
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');
47  
48 class SugarLogger implements LoggerTemplate
49 {
50     /**
51      * properties for the SugarLogger
52      */
53         private $logfile = 'sugarcrm';
54         private $ext = '.log';
55         private $dateFormat = '%c';
56         private $logSize = '10MB';
57         private $maxLogs = 10;
58         private $filesuffix = "";
59         private $log_dir = '.';
60
61         
62         /**
63          * used for config screen
64          */
65         public static $filename_suffix = array(
66             "%m_%Y"    => "Month_Year", 
67             "%w_%m"    => "Week_Month",
68             "%m_%d_%y" => "Month_Day_Year",
69             );
70         
71         /**
72          * Let's us know if we've initialized the logger file
73          */
74     private $initialized = false;
75     
76     /**
77      * Logger file handle
78      */
79     private $fp = false;
80     
81     public function __get(
82         $key
83         )
84     {
85         return $this->$key;
86     }
87         
88     /**
89      * Used by the diagnostic tools to get SugarLogger log file information
90      */
91     public function getLogFileNameWithPath()
92     {
93         return $this->full_log_file;
94     }
95         
96     /**
97      * Used by the diagnostic tools to get SugarLogger log file information
98      */
99     public function getLogFileName()
100     {
101         return ltrim($this->full_log_file, "./");
102     }
103     
104     /**
105      * Constructor
106      *
107      * Reads the config file for logger settings
108      */
109     public function __construct() 
110     {
111         $config = SugarConfig::getInstance();
112         $this->ext = $config->get('logger.file.ext', $this->ext);
113         $this->logfile = $config->get('logger.file.name', $this->logfile);
114         $this->dateFormat = $config->get('logger.file.dateFormat', $this->dateFormat);
115         $this->logSize = $config->get('logger.file.maxSize', $this->logSize);
116         $this->maxLogs = $config->get('logger.file.maxLogs', $this->maxLogs);
117         $this->filesuffix = $config->get('logger.file.suffix', $this->filesuffix);
118         $log_dir = $config->get('log_dir' , $this->log_dir); 
119         $this->log_dir = $log_dir . (empty($log_dir)?'':'/');
120         unset($config);
121         $this->_doInitialization();
122         LoggerManager::setLogger('default','SugarLogger');
123         }
124         
125         /**
126          * Handles the SugarLogger initialization
127          */
128     private function _doInitialization() 
129     {
130         $this->full_log_file = $this->log_dir . $this->logfile . $this->ext;
131         $this->initialized = $this->_fileCanBeCreatedAndWrittenTo();
132         $this->rollLog();
133     }
134
135     /**
136          * Checks to see if the SugarLogger file can be created and written to
137          */
138     private function _fileCanBeCreatedAndWrittenTo() 
139     {
140         $this->_attemptToCreateIfNecessary();
141         return file_exists($this->full_log_file) && is_writable($this->full_log_file);
142     }
143
144     /**
145          * Creates the SugarLogger file if it doesn't exist
146          */
147     private function _attemptToCreateIfNecessary() 
148     {
149         if (file_exists($this->full_log_file)) {
150             return;
151         }
152         @touch($this->full_log_file);
153     }
154     
155     /**
156      * see LoggerTemplate::log()
157      */
158         public function log(
159             $level,
160             $message
161             ) 
162         {
163         if (!$this->initialized) {
164             return;
165         }
166                 //lets get the current user id or default to -none- if it is not set yet
167                 $userID = (!empty($GLOBALS['current_user']->id))?$GLOBALS['current_user']->id:'-none-';
168
169                 //if we haven't opened a file pointer yet let's do that
170                 if (! $this->fp)$this->fp = fopen ($this->full_log_file , 'a' );
171
172                 
173                 // change to a string if there is just one entry
174             if ( is_array($message) && count($message) == 1 )
175                 $message = array_shift($message);
176             // change to a human-readable array output if it's any other array
177             if ( is_array($message) )
178                     $message = print_r($message,true);
179                 
180                 //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
181                 fwrite($this->fp, 
182                     strftime($this->dateFormat) . ' [' . getmypid () . '][' . $userID . '][' . strtoupper($level) . '] ' . $message . "\n" 
183                     );
184         }
185         
186         /**
187          * rolls the logger file to start using a new file
188          */
189         private function rollLog(
190             $force = false
191             ) 
192         {
193         if (!$this->initialized || empty($this->logSize)) {
194             return;
195         }
196                 // lets get the number of megs we are allowed to have in the file
197                 $megs = substr ( $this->logSize, 0, strlen ( $this->logSize ) - 2 );
198                 //convert it to bytes
199                 $rollAt = ( int ) $megs * 1024 * 1024;
200                 //check if our log file is greater than that or if we are forcing the log to roll
201                 if ($force || filesize ( $this->full_log_file ) >= $rollAt) {
202                         //now lets move the logs starting at the oldest and going to the newest
203                         for($i = $this->maxLogs - 2; $i > 0; $i --) {
204                                 if (file_exists ( $this->log_dir . $this->logfile . $i . $this->ext )) {
205                                         $to = $i + 1;
206                                         $old_name = $this->log_dir . $this->logfile . $i . $this->ext;
207                                         $new_name = $this->log_dir . $this->logfile . $to . $this->ext;
208                                         //nsingh- Bug 22548  Win systems fail if new file name already exists. The fix below checks for that.
209                                         //if/else branch is necessary as suggested by someone on php-doc ( see rename function ).
210                                         sugar_rename($old_name, $new_name);
211
212                                         //rename ( $this->logfile . $i . $this->ext, $this->logfile . $to . $this->ext );
213                                 }
214                         }
215                         //now lets move the current .log file
216                         sugar_rename ($this->full_log_file, $this->log_dir . $this->logfile . '1' . $this->ext);
217
218                 }
219         }
220         
221         /**
222          * Destructor
223          *
224          * Closes the SugarLogger file handle
225      */
226         public function __destruct() 
227         {
228                 if ($this->fp)
229                         fclose($this->fp);
230         }
231 }