1 | <?php |
||
52 | abstract class Logger |
||
53 | { |
||
54 | |||
55 | /** |
||
56 | * Resource hacia el Archivo del Log |
||
57 | * |
||
58 | * @var resource |
||
59 | */ |
||
60 | private static $fileLogger; |
||
61 | /** |
||
62 | * @var |
||
63 | */ |
||
64 | private static $log_name = null; |
||
65 | /** |
||
66 | * Indica si hay transaccion o no |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | private static $transaction = false; |
||
71 | /** |
||
72 | * Array con mensajes de log en cola en una transsaccion |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private static $queue = array(); |
||
77 | /** |
||
78 | * Path donde se guardaran los logs |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private static $log_path = ''; |
||
83 | |||
84 | /** |
||
85 | * Inicializa el Logger |
||
86 | * |
||
87 | * @param string $name |
||
88 | */ |
||
89 | public static function initialize($name = '') |
||
90 | { |
||
91 | self::$log_path = APP_PATH . 'temp/logs/'; //TODO poder cambiar el path |
||
92 | if ($name === '') { |
||
93 | $name = 'log' . date('Y-m-d') . '.txt'; |
||
94 | } |
||
95 | self::$fileLogger = fopen(self::$log_path . $name, 'a'); |
||
96 | if (!self::$fileLogger) { |
||
97 | throw new KumbiaException("No se puede abrir el log llamado: " . $name); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Especifica el PATH donde se guardan los logs |
||
103 | * |
||
104 | * @param string $path |
||
105 | */ |
||
106 | public static function set_path($path) |
||
110 | |||
111 | /** |
||
112 | * Obtener el path actual |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public static function get_path() |
||
120 | |||
121 | /** |
||
122 | * Almacena un mensaje en el log |
||
123 | * |
||
124 | * @param string $type |
||
125 | * @param string $msg |
||
126 | * @param string $name_log |
||
127 | */ |
||
128 | public static function log($type = 'DEBUG', $msg, $name_log) |
||
129 | { |
||
130 | if (is_array($msg)) { |
||
131 | $msg = print_r($msg, true); |
||
132 | } |
||
133 | //TODO poder añadir otros formatos de log |
||
134 | $date = date(DATE_RFC1036); |
||
135 | $msg = "[$date][$type] " . $msg; |
||
136 | if (self::$transaction) { |
||
137 | self::$queue[] = $msg; |
||
138 | return; |
||
139 | } |
||
140 | self::write($msg, $name_log); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Escribir en el log |
||
145 | * |
||
146 | * @param string $msg |
||
147 | */ |
||
148 | protected static function write($msg, $name_log) |
||
149 | { |
||
150 | self::initialize($name_log); //TODO dejarlo abierto cuando es un commit |
||
151 | fputs(self::$fileLogger, $msg . PHP_EOL); |
||
152 | self::close(); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Inicia una transacción |
||
157 | * |
||
158 | */ |
||
159 | public static function begin() |
||
163 | |||
164 | /** |
||
165 | * Deshace una transacción |
||
166 | * |
||
167 | */ |
||
168 | public static function rollback() |
||
173 | |||
174 | /** |
||
175 | * Commit a una transacción |
||
176 | * |
||
177 | * @param string $name_log |
||
178 | */ |
||
179 | public static function commit($name_log = '') |
||
187 | |||
188 | /** |
||
189 | * Cierra el Logger |
||
190 | * |
||
191 | */ |
||
192 | public static function close() |
||
199 | |||
200 | /** |
||
201 | * Genera un log de tipo WARNING |
||
202 | * |
||
203 | * @return |
||
204 | * @param string $msg |
||
205 | * @param string $name_log |
||
206 | */ |
||
207 | public static function warning($msg, $name_log = '') |
||
211 | |||
212 | /** |
||
213 | * Genera un log de tipo ERROR |
||
214 | * |
||
215 | * @return |
||
216 | * @param string $msg |
||
217 | * @param string $name_log |
||
218 | */ |
||
219 | public static function error($msg, $name_log = '') |
||
220 | { |
||
221 | self::log('ERROR', $msg, $name_log); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Genera un log de tipo DEBUG |
||
226 | * |
||
227 | * @return |
||
228 | * @param string $msg |
||
229 | * @param string $name_log |
||
230 | */ |
||
231 | public static function debug($msg, $name_log = '') |
||
232 | { |
||
233 | self::log('DEBUG', $msg, $name_log); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Genera un log de tipo ALERT |
||
238 | * |
||
239 | * @return |
||
240 | * @param string $msg |
||
241 | * @param string $name_log |
||
242 | */ |
||
243 | public static function alert($msg, $name_log = '') |
||
247 | |||
248 | /** |
||
249 | * Genera un log de tipo CRITICAL |
||
250 | * |
||
251 | * @return |
||
252 | * @param string $msg |
||
253 | * @param string $name_log |
||
254 | */ |
||
255 | public static function critical($msg, $name_log = '') |
||
259 | |||
260 | /** |
||
261 | * Genera un log de tipo NOTICE |
||
262 | * |
||
263 | * @return |
||
264 | * @param string $msg |
||
265 | * @param string $name_log |
||
266 | */ |
||
267 | public static function notice($msg, $name_log = '') |
||
271 | |||
272 | /** |
||
273 | * Genera un log de tipo INFO |
||
274 | * |
||
275 | * @return |
||
276 | * @param string $msg |
||
277 | * @param string $name_log |
||
278 | */ |
||
279 | public static function info($msg, $name_log = '') |
||
280 | { |
||
281 | self::log('INFO', $msg, $name_log); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Genera un log de tipo EMERGENCE |
||
286 | * |
||
287 | * @return |
||
288 | * @param string $msg |
||
289 | * @param string $name_log |
||
290 | */ |
||
291 | public static function emergence($msg, $name_log = '') |
||
295 | |||
296 | /** |
||
297 | * Genera un log Personalizado |
||
298 | * |
||
299 | * @param string $type |
||
300 | * @param string $msg |
||
301 | * @param string $name_log |
||
302 | */ |
||
303 | public static function custom($type = 'CUSTOM', $msg, $name_log = '') |
||
307 | |||
308 | } |
||
309 |