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