-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
66 lines (59 loc) · 2.99 KB
/
logger.js
File metadata and controls
66 lines (59 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const winston = require('winston');
const path = require('path');
const fs = require('fs');
const baseDir = process.cwd(); // process.cwd() apunta al directorio donde se ejecuta el comando node
// Ruta del directorio de logs
const logDir = path.join(baseDir, 'log');
// Verifica si el directorio existe, si no, lo crea
if (!fs.existsSync(logDir)) {
console.log("[" + new Date().toLocaleString() +"]: Directorio para log no existe, creando:", logDir);
fs.mkdirSync(logDir);
} else {
console.log("[" + new Date().toLocaleString() +"]: Directorio para log ya existe:", logDir);
}
try {
const logger = new (winston.Logger)({
level: 'info', // Nivel de log predeterminado
transports: [
// Transporte para imprimir en la consola
new (winston.transports.Console)({
timestamp: () => new Date().toLocaleString(), // Agrega timestamp
formatter: (options) => {
return `[${options.timestamp()}]: ${options.message || ''}`;
}
}),
// Transporte para guardar logs generales en un archivo
new (winston.transports.File)({
name: 'general-file', // Nombre único para este transporte
filename: path.join(logDir, 'salida.log'), // Ruta del archivo de log
level: 'info', // Nivel de log para el archivo
maxsize: 5 * 1024 * 1024, // Tamaño máximo del archivo (5 MB)
maxFiles: 5, // Número máximo de archivos rotativos
tailable: true, // Mantiene los archivos rotativos en orden
json: false, // Deshabilita el formato JSON
timestamp: () => new Date().toLocaleString(), // Agrega timestamp
formatter: (options) => {
return `[${options.timestamp()}]: ${options.message || ''}`;
}
}),
// Transporte para guardar solo errores en un archivo separado
new (winston.transports.File)({
name: 'error-file', // Nombre único para este transporte
filename: path.join(logDir, 'error.log'), // Archivo específico para errores
level: 'error', // Solo registra mensajes de nivel error
maxsize: 5 * 1024 * 1024, // Tamaño máximo del archivo (5 MB)
maxFiles: 5, // Número máximo de archivos rotativos
tailable: true, // Mantiene los archivos rotativos en orden
json: false, // Deshabilita el formato JSON
timestamp: () => new Date().toLocaleString(), // Agrega timestamp
formatter: (options) => {
// Personaliza el formato para que solo incluya el timestamp y el mensaje
return `[${options.timestamp()}]: ${options.message || ''}`;
}
})
]
});
module.exports = logger;
} catch (error) {
console.error("[" + new Date().toLocaleString() +"]: Error al configurar el logger:", error);
}