| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /** Kumbia - PHP Rapid Development Framework *****************************
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (C) 2005-2007 Andrés Felipe Gutiérrez (andresfelipe at vagoogle.net)
|
|---|
| 6 | *
|
|---|
| 7 | * This framework is free software; you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 9 | * License as published by the Free Software Foundation; either
|
|---|
| 10 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This framework is distributed in the hope that it will be useful,
|
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | * Lesser General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 18 | * License along with this framework; if not, write to the Free Software
|
|---|
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 20 | *
|
|---|
| 21 | * Este framework es software libre; puedes redistribuirlo y/o modificarlo
|
|---|
| 22 | * bajo los terminos de la licencia pública general GNU tal y como fue publicada
|
|---|
| 23 | * por la Fundación del Software Libre; desde la versión 2.1 o cualquier
|
|---|
| 24 | * versión superior.
|
|---|
| 25 | *
|
|---|
| 26 | * Este framework es distribuido con la esperanza de ser util pero SIN NINGUN
|
|---|
| 27 | * TIPO DE GARANTIA; sin dejar atrás su LADO MERCANTIL o PARA FAVORECER ALGUN
|
|---|
| 28 | * FIN EN PARTICULAR. Lee la licencia publica general para más detalles.
|
|---|
| 29 | *
|
|---|
| 30 | * Debes recibir una copia de la Licencia Pública General GNU junto con este
|
|---|
| 31 | * framework, si no es asi, escribe a Fundación del Software Libre Inc.,
|
|---|
| 32 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 33 | *
|
|---|
| 34 | *****************************************************************************
|
|---|
| 35 | /**
|
|---|
| 36 | * Permite realizar logs en archivos de texto en la carpeta Logs
|
|---|
| 37 | * $fileLogger = Es el File Handle para escribir los logs
|
|---|
| 38 | * $transaction = Indica si hay o no transaccion
|
|---|
| 39 | * $quenue = array con lista de logs pendientes
|
|---|
| 40 | *
|
|---|
| 41 | * Ej:
|
|---|
| 42 | *
|
|---|
| 43 | * //Empieza un log en logs/logDDMMY.txt
|
|---|
| 44 | * $myLog = new Logger();
|
|---|
| 45 | *
|
|---|
| 46 | * $myLog->log("Loggear esto como un debug", Logger::DEBUG);
|
|---|
| 47 | *
|
|---|
| 48 | * //Esto se guarda al log inmediatamente
|
|---|
| 49 | * $myLog->log("Loggear esto como un error", Logger::ERROR);
|
|---|
| 50 | *
|
|---|
| 51 | * //Inicia una transacción
|
|---|
| 52 | * $myLog->begin();
|
|---|
| 53 | *
|
|---|
| 54 | * //Esto queda pendiente hasta que se llame a commit para guardar
|
|---|
| 55 | * //ó rollback para cancelar
|
|---|
| 56 | * $myLog->log("Esto es un log en la fila", Logger::WARNING)
|
|---|
| 57 | * $myLog->log("Esto es un otro log en la fila", Logger::WARNING)*
|
|---|
| 58 | *
|
|---|
| 59 | * //Se guarda al log
|
|---|
| 60 | * $myLog->commit();
|
|---|
| 61 | *
|
|---|
| 62 | * //Cierra el Log
|
|---|
| 63 | * $myLog->close();
|
|---|
| 64 | */
|
|---|
| 65 | class Logger {
|
|---|
| 66 |
|
|---|
| 67 | private $fileLogger;
|
|---|
| 68 | private $transaction = false;
|
|---|
| 69 | private $quenue = array();
|
|---|
| 70 |
|
|---|
| 71 | const DEBUG = 1;
|
|---|
| 72 | const ERROR = 2;
|
|---|
| 73 | const WARNING = 3;
|
|---|
| 74 | const CUSTOM = 4;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Constructor del Logger
|
|---|
| 78 | */
|
|---|
| 79 | function Logger($name=''){
|
|---|
| 80 | if($name===''||$name===true){
|
|---|
| 81 | $name = 'log'.date('dmY').".txt";
|
|---|
| 82 | }
|
|---|
| 83 | $this->fileLogger = @fopen('logs/'.$name, "a");
|
|---|
| 84 | if(!$this->fileLogger){
|
|---|
| 85 | error('KumbiaLogger: Cannot Open Log '.$name);
|
|---|
| 86 | return false;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | /**
|
|---|
| 90 | * Almacena un mensaje en el log
|
|---|
| 91 | *
|
|---|
| 92 | * @param string $msg
|
|---|
| 93 | */
|
|---|
| 94 | function log($msg, $type=self::DEBUG){
|
|---|
| 95 | if(!$this->fileLogger){
|
|---|
| 96 | error('KumbiaLogger: Cannot handle log on an invalid logger');
|
|---|
| 97 | }
|
|---|
| 98 | if(PHP_VERSION>=5.1) {
|
|---|
| 99 | $date = date(DATE_RFC1036);
|
|---|
| 100 | } else {
|
|---|
| 101 | $date = date("r");
|
|---|
| 102 | }
|
|---|
| 103 | switch($type){
|
|---|
| 104 | case self::DEBUG:
|
|---|
| 105 | $type = 'DEBUG';
|
|---|
| 106 | break;
|
|---|
| 107 | case self::ERROR:
|
|---|
| 108 | $type = 'ERROR';
|
|---|
| 109 | break;
|
|---|
| 110 | case self::WARNING:
|
|---|
| 111 | $type = 'WARNING';
|
|---|
| 112 | break;
|
|---|
| 113 | case self::CUSTOM :
|
|---|
| 114 | $type = 'CUSTOM';
|
|---|
| 115 | break;
|
|---|
| 116 | default:
|
|---|
| 117 | $type = 'CUSTOM';
|
|---|
| 118 | }
|
|---|
| 119 | if($this->transaction){
|
|---|
| 120 | $this->quenue[] = "[$date][$type] ".$msg."\n";
|
|---|
| 121 | } else {
|
|---|
| 122 | fputs($this->fileLogger, "[$date][$type] ".$msg."\n");
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * Inicia una transacción
|
|---|
| 128 | *
|
|---|
| 129 | */
|
|---|
| 130 | function begin(){
|
|---|
| 131 | $this->transaction = true;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Deshace una transacción
|
|---|
| 136 | *
|
|---|
| 137 | */
|
|---|
| 138 | function rollback(){
|
|---|
| 139 | $this->transaction = false;
|
|---|
| 140 | $this->quenue = array();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Commit a una transacción
|
|---|
| 145 | */
|
|---|
| 146 | function commit(){
|
|---|
| 147 | $this->transaction = false;
|
|---|
| 148 | foreach($this->quenue as $msg){
|
|---|
| 149 | $this->log($msg);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Cierra el Logger
|
|---|
| 155 | *
|
|---|
| 156 | */
|
|---|
| 157 | function close(){
|
|---|
| 158 | if(!$this->fileLogger){
|
|---|
| 159 | error('KumbiaLogger: Cannot handle log on an invalid logger');
|
|---|
| 160 | }
|
|---|
| 161 | return fclose($this->fileLogger);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | ?> |
|---|