KumbiaPHP beta2-dev
Framework PHP en español
logger.php
Ir a la documentación de este archivo.
00001 <?php
00055 abstract class Logger {
00056 
00062         private static $fileLogger;
00066     private static $log_name = null;
00067 
00073         private static $transaction = false;
00074 
00080         private static $quenue = array();
00081 
00087         private static $log_path = '';
00088 
00092         public static function initialize($name='')
00093     {
00094         self::$log_path = APP_PATH . 'temp/logs/';
00095                 if($name===''||$name===true){
00096                         $name = 'log'.date('dmY').'.txt';
00097                 }
00098                 self::$fileLogger = fopen(self::$log_path.$name, 'a');
00099                 if(!self::$fileLogger){
00100                         throw new KumbiaException("No se puede abrir el log llamado: ".$name);
00101                         return false;
00102                 }
00103         }
00109         public static function set_path($path)
00110     {
00111                 self::$log_path = $path;
00112         }
00113 
00119         public static function get_path()
00120     {
00121                 return self::$log_path;
00122         }
00123 
00124 
00132         public static function log($type='DEBUG', $msg, $name_log)
00133     {
00134             self::initialize($name_log);
00135                 if(!self::$fileLogger){
00136                         throw new KumbiaException('No se puede enviar mensaje al log porque es invalido');
00137                 }
00138                 if(is_array($msg)){
00139                         $msg = print_r($msg, true);
00140                 }
00141                 $date = date(DATE_RFC1036);
00142                 if(self::$transaction){
00143                         self::$quenue[] = "[$date][$type] ".$msg;
00144                 } else {
00145                         fputs(self::$fileLogger, "[$date][$type] ".$msg.PHP_EOL);
00146                 }
00147         self::close();
00148         }
00149 
00154         public static function begin()
00155     {
00156                 self::$transaction = true;
00157         }
00158 
00163         public static function rollback()
00164     {
00165                 self::$transaction = false;
00166                 self::$quenue = array();
00167         }
00168 
00172         public static function commit()
00173     {
00174                 self::$transaction = false;
00175                 foreach(self::$quenue as $msg){
00176                         self::log($msg);
00177                 }
00178         }
00179 
00184         public static function close()
00185     {
00186                 if(!self::$fileLogger){
00187                         throw new KumbiaException("No se puede cerrar el log porque es invalido");
00188                 }
00189                 return fclose(self::$fileLogger);
00190         }
00198     public static function warning ($msg, $name_log='')
00199     {
00200         self::log('WARNING', $msg, $name_log);
00201     }
00202     
00210     public static function error ($msg, $name_log='')
00211     {
00212         self::log('ERROR', $msg, $name_log);
00213     }
00221     public static function debug ($msg, $name_log='')
00222     {
00223         self::log('DEBUG', $msg, $name_log);
00224     }
00232     public static function alert ($msg, $name_log='')
00233     {
00234         self::log('ALERT', $msg, $name_log);
00235     }
00243     public static function critical ($msg, $name_log='')
00244     {
00245         self::log('CRITICAL', $msg, $name_log);
00246     }
00254     public static function notice ($msg, $name_log='')
00255     {
00256         self::log('NOTICE', $msg, $name_log);
00257     }
00265     public static function info ($msg, $name_log='')
00266     {
00267         self::log('INFO', $msg, $name_log);
00268     }
00276     public static function emergence ($msg, $name_log='')
00277     {
00278         self::log('EMERGENCE', $msg, $name_log);
00279     }
00287     public static function custom ($type='CUSTOM', $msg, $name_log='')
00288     {
00289         self::log($type, $msg, $name_log);
00290     }
00291 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones