]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarLogger/LoggerManager.php
Release 6.5.8
[Github/sugarcrm.git] / include / SugarLogger / LoggerManager.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-2012 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  * Log management
40  * @api
41  */
42 class LoggerManager
43 {
44         //this the the current log level
45         private $_level = 'fatal';
46
47         //this is a list of different loggers that have been loaded
48         private static $_loggers = array();
49
50         //this is the instance of the LoggerManager
51         private static $_instance = NULL;
52
53         //these are the mappings for levels to different log types
54         private static $_logMapping = array(
55                 'default' => 'SugarLogger',
56         );
57
58         //these are the log level mappings anything with a lower value than your current log level will be logged
59         private static $_levelMapping = array(
60                 'debug'      => 100,
61                 'info'       => 70,
62                 'warn'       => 50,
63                 'deprecated' => 40,
64                 'error'      => 25,
65                 'fatal'      => 10,
66                 'security'   => 5,
67                 'off'        => 0,
68         );
69
70         //only let the getLogger instantiate this object
71         private function __construct()
72         {
73                 $level = SugarConfig::getInstance()->get('logger.level', $this->_level);
74                 if (!empty($level))
75                         $this->setLevel($level);
76
77                 if ( empty(self::$_loggers) )
78                     $this->_findAvailableLoggers();
79         }
80
81         /**
82          * Overloaded method that handles the logging requests.
83          *
84          * @param string $method
85          * @param string $message - also handles array as parameter, though that is deprecated.
86          */
87         public function __call(
88             $method,
89             $message
90             )
91         {
92         if ( !isset(self::$_levelMapping[$method]) )
93             $method = $this->_level;
94                 //if the method is a direct match to our level let's let it through this allows for custom levels
95                 if($method == $this->_level
96                 //otherwise if we have a level mapping for the method and that level is less than or equal to the current level let's let it log
97                 || (!empty(self::$_levelMapping[$method])
98                     && self::$_levelMapping[$this->_level] >= self::$_levelMapping[$method]) ) {
99                         //now we get the logger type this allows for having a file logger an email logger, a firebug logger or any other logger you wish you can set different levels to log differently
100                         $logger = (!empty(self::$_logMapping[$method])) ?
101                             self::$_logMapping[$method] : self::$_logMapping['default'];
102                         //if we haven't instantiated that logger let's instantiate
103                         if (!isset(self::$_loggers[$logger])) {
104                             self::$_loggers[$logger] = new $logger();
105                         }
106                         //tell the logger to log the message
107                         self::$_loggers[$logger]->log($method, $message);
108                 }
109         }
110
111         /**
112          * Check if this log level will be producing any logging
113          * @param string $method
114          * @return boolean
115          */
116         public function wouldLog($method)
117         {
118             if ( !isset(self::$_levelMapping[$method]) )
119                 $method = $this->_level;
120             if($method == $this->_level
121                         //otherwise if we have a level mapping for the method and that level is less than or equal to the current level let's let it log
122                         || (!empty(self::$_levelMapping[$method])
123                                         && self::$_levelMapping[$this->_level] >= self::$_levelMapping[$method]) ) {
124                 return true;
125             }
126             return false;
127         }
128
129         /**
130      * Used for doing design-by-contract assertions in the code; when the condition fails we'll write
131      * the message to the debug log
132      *
133      * @param string  $message
134      * @param boolean $condition
135      */
136     public function assert(
137         $message,
138         $condition
139         )
140     {
141         if ( !$condition )
142             $this->__call('debug', $message);
143         }
144
145         /**
146          * Sets the logger to the level indicated
147          *
148          * @param string $name name of logger level to set it to
149          */
150         public function setLevel(
151             $name
152             )
153         {
154         if ( isset(self::$_levelMapping[$name]) )
155             $this->_level = $name;
156         }
157
158         /**
159          * Returns a logger instance
160          */
161         public static function getLogger()
162         {
163                 if(!LoggerManager::$_instance){
164                         LoggerManager::$_instance = new LoggerManager();
165                 }
166                 return LoggerManager::$_instance;
167         }
168
169         /**
170          * Sets the logger to use a particular backend logger for the given level. Set level to 'default'
171          * to make it the default logger for the application
172          *
173          * @param string $level name of logger level to set it to
174          * @param string $logger name of logger class to use
175          */
176         public static function setLogger(
177             $level,
178             $logger
179             )
180         {
181             self::$_logMapping[$level] = $logger;
182         }
183
184         /**
185          * Finds all the available loggers in the application
186          */
187         protected function _findAvailableLoggers()
188         {
189             $locations = array('include/SugarLogger','custom/include/SugarLogger');
190             foreach ( $locations as $location ) {
191             if (sugar_is_dir($location) && $dir = opendir($location)) {
192                 while (($file = readdir($dir)) !== false) {
193                     if ($file == ".."
194                             || $file == "."
195                             || $file == "LoggerTemplate.php"
196                             || $file == "LoggerManager.php"
197                             || !is_file("$location/$file")
198                             )
199                         continue;
200                     require_once("$location/$file");
201                     $loggerClass = basename($file, ".php");
202                     if ( class_exists($loggerClass) && class_implements($loggerClass,'LoggerTemplate') )
203                         self::$_loggers[$loggerClass] = new $loggerClass();
204                 }
205             }
206         }
207         }
208
209         public static function getAvailableLoggers()
210         {
211             return array_keys(self::$_loggers);
212         }
213
214         public static function getLoggerLevels()
215         {
216             $loggerLevels = self::$_levelMapping;
217             foreach ( $loggerLevels as $key => $value )
218                 $loggerLevels[$key] = ucfirst($key);
219
220             return $loggerLevels;
221         }
222 }