KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
logger.php
Ir a la documentación de este archivo.
1 <?php
56 abstract class Logger
57 {
58 
64  private static $fileLogger;
68  private static $log_name = null;
74  private static $transaction = false;
80  private static $quenue = array();
86  private static $log_path = '';
87 
91  public static function initialize($name='')
92  {
93  self::$log_path = APP_PATH . 'temp/logs/';
94  if ($name === '' || $name === true) {
95  $name = 'log' . date('Y-m-d') . '.txt';
96  }
97  self::$fileLogger = fopen(self::$log_path . $name, 'a');
98  if (!self::$fileLogger) {
99  throw new KumbiaException("No se puede abrir el log llamado: " . $name);
100  }
101  }
102 
108  public static function set_path($path)
109  {
110  self::$log_path = $path;
111  }
112 
118  public static function get_path()
119  {
120  return self::$log_path;
121  }
122 
130  public static function log($type='DEBUG', $msg, $name_log)
131  {
132  self::initialize($name_log);
133  if (!self::$fileLogger) {
134  throw new KumbiaException('No se puede enviar mensaje al log porque es invalido');
135  }
136  if (is_array($msg)) {
137  $msg = print_r($msg, true);
138  }
139  $date = date(DATE_RFC1036);
140  if (self::$transaction) {
141  self::$quenue[] = "[$date][$type] " . $msg;
142  } else {
143  fputs(self::$fileLogger, "[$date][$type] " . $msg . PHP_EOL);
144  }
145  self::close();
146  }
147 
152  public static function begin()
153  {
154  self::$transaction = true;
155  }
156 
161  public static function rollback()
162  {
163  self::$transaction = false;
164  self::$quenue = array();
165  }
166 
170  public static function commit()
171  {
172  self::$transaction = false;
173  foreach (self::$quenue as $msg) {
174  self::log($msg);
175  }
176  }
177 
182  public static function close()
183  {
184  if (!self::$fileLogger) {
185  throw new KumbiaException("No se puede cerrar el log porque es invalido");
186  }
187  return fclose(self::$fileLogger);
188  }
189 
197  public static function warning($msg, $name_log='')
198  {
199  self::log('WARNING', $msg, $name_log);
200  }
201 
209  public static function error($msg, $name_log='')
210  {
211  self::log('ERROR', $msg, $name_log);
212  }
213 
221  public static function debug($msg, $name_log='')
222  {
223  self::log('DEBUG', $msg, $name_log);
224  }
225 
233  public static function alert($msg, $name_log='')
234  {
235  self::log('ALERT', $msg, $name_log);
236  }
237 
245  public static function critical($msg, $name_log='')
246  {
247  self::log('CRITICAL', $msg, $name_log);
248  }
249 
257  public static function notice($msg, $name_log='')
258  {
259  self::log('NOTICE', $msg, $name_log);
260  }
261 
269  public static function info($msg, $name_log='')
270  {
271  self::log('INFO', $msg, $name_log);
272  }
273 
281  public static function emergence($msg, $name_log='')
282  {
283  self::log('EMERGENCE', $msg, $name_log);
284  }
285 
293  public static function custom($type='CUSTOM', $msg, $name_log='')
294  {
295  self::log($type, $msg, $name_log);
296  }
297 
298 }