Completed
Push — master ( 23b69b...8aca2f )
by joanhey
14s queued 10s
created

Logger::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Logger
12
 *
13
 * @copyright  Copyright (c) 2005 - 2020 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Permite realizar logs en archivos de texto en la carpeta Logs
19
 *
20
 * $fileLogger = Es el File Handle para escribir los logs
21
 * $transaction = Indica si hay o no transaccion
22
 * $queue = array con lista de logs pendientes
23
 *
24
 * Ej:
25
 * <code>
26
 * //Empieza un log en logs/logDDMMYY.txt
27
 *
28
 *
29
 * Logger::debug('Loggear esto como un debug');
30
 *
31
 * //Esto se guarda al log inmediatamente
32
 * Logger::error('Loggear esto como un error');
33
 *
34
 * //Inicia una transacción
35
 * Logger::begin();
36
 *
37
 * //Esto queda pendiente hasta que se llame a commit para guardar
38
 * //ó rollback para cancelar
39
 * Logger::warning('Esto es un log en la fila');
40
 * Logger::notice('Esto es un otro log en la fila');
41
 *
42
 * //Se guarda al log
43
 * Logger::commit();
44
 *
45
 * //Cierra el Log
46
 * Logger::close();
47
 * </code>
48
 *
49
 * @category   Kumbia
50
 * @package    Logger
51
 */
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)
107
    {
108
        self::$log_path = $path;
109
    }
110
111
    /**
112
     * Obtener el path actual
113
     *
114
     * @return string
115
     */
116
    public static function get_path()
117
    {
118
        return self::$log_path;
119
    }
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()
160
    {
161
        self::$transaction = true;
162
    }
163
164
    /**
165
     * Deshace una transacción
166
     *
167
     */
168
    public static function rollback()
169
    {
170
        self::$transaction = false;
171
        self::$queue = array();
172
    }
173
174
    /**
175
     * Commit a una transacción
176
     *
177
     * @param string $name_log
178
     */
179
    public static function commit($name_log = '')
180
    {
181
        foreach (self::$queue as $msg) {
182
            self::write($msg, $name_log);
183
        }
184
        self::$queue = array();
185
        self::$transaction = false;
186
    }
187
188
    /**
189
     * Cierra el Logger
190
     *
191
     */
192
    public static function close()
193
    {
194
        if (!self::$fileLogger) {
195
            throw new KumbiaException("No se puede cerrar el log porque es invalido");
196
        }
197
        return fclose(self::$fileLogger);
198
    }
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 = '')
208
    {
209
        self::log('WARNING', $msg, $name_log);
210
    }
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 = '')
244
    {
245
        self::log('ALERT', $msg, $name_log);
246
    }
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 = '')
256
    {
257
        self::log('CRITICAL', $msg, $name_log);
258
    }
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 = '')
268
    {
269
        self::log('NOTICE', $msg, $name_log);
270
    }
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 = '')
292
    {
293
        self::log('EMERGENCE', $msg, $name_log);
294
    }
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 = '')
304
    {
305
        self::log($type, $msg, $name_log);
306
    }
307
308
}
309