Skip to content

Commit 53aef0d

Browse files
committed
25 - js - LOGS
1 parent 3ed2f9d commit 53aef0d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
_____________________________________
3+
https://github.com/kenysdev
4+
2024 - JavaScript
5+
_______________________________________
6+
#25 LOGS
7+
---------------------------------------
8+
* EJERCICIO:
9+
 * Explora el concepto de "logging" en tu lenguaje. Configúralo y muestra
10+
 * un ejemplo con cada nivel de "severidad" disponible.
11+
 *
12+
 * DIFICULTAD EXTRA (opcional):
13+
 * Crea un programa ficticio de gestión de tareas que permita añadir, eliminar
14+
 * y listar dichas tareas.
15+
 * - Añadir: recibe nombre y descripción.
16+
 * - Eliminar: por nombre de la tarea.
17+
 * Implementa diferentes mensajes de log que muestren información según la
18+
 * tarea ejecutada (a tu elección).
19+
 * Utiliza el log para visualizar el tiempo de ejecución de cada tarea.
20+
*/
21+
// ________________________________________________________
22+
23+
import { createLogger, transports, format } from 'winston';
24+
25+
const logger = createLogger({
26+
level: 'debug',
27+
format: format.combine(
28+
format.timestamp(),
29+
format.printf(({ timestamp, level, message }) => {
30+
return `${timestamp} - ${level} - ${message}`;
31+
})
32+
),
33+
transports: [
34+
new transports.Console()
35+
]
36+
});
37+
38+
logger.debug('Depuración');
39+
40+
logger.info('Informativo');
41+
42+
logger.warn('Advertencia');
43+
44+
logger.error('Error');
45+
logger.error('Error crítico');
46+
47+
// ________________________________________________________
48+
console.log("\nDIFICULTAD EXTRA");
49+
50+
function showTime(func) {
51+
return async function (...args) {
52+
const startTime = Date.now();
53+
await func.apply(this, args);
54+
const endTime = Date.now();
55+
const executionTime = (endTime - startTime) / 1000;
56+
logger.debug(`Tiempo de ejecución de ${func.name}: ${executionTime.toFixed(21)} segundos.`);
57+
};
58+
}
59+
60+
class Program {
61+
constructor() {
62+
this.tasks = {};
63+
logger.debug('Se inició instancia de la clase.');
64+
}
65+
66+
add = showTime(async function (name, description) {
67+
this.tasks[name] = description;
68+
logger.info('Se agregó una tarea.');
69+
});
70+
71+
delete = showTime(async function (name) {
72+
if (name in this.tasks) {
73+
delete this.tasks[name];
74+
logger.info(`Tarea '${name}' eliminada.`);
75+
} else {
76+
logger.warn(`No se encontró la tarea '${name}'.`);
77+
}
78+
});
79+
80+
showList = showTime(async function () {
81+
logger.info('Lista de tareas');
82+
for (const [task, des] of Object.entries(this.tasks)) {
83+
console.log(`${task} -- ${des}`);
84+
}
85+
});
86+
}
87+
88+
const tasks = new Program();
89+
90+
(async () => {
91+
await tasks.add("a", ".1");
92+
await tasks.add("b", "2");
93+
await tasks.add("c", "3");
94+
95+
await tasks.delete("a");
96+
await tasks.showList();
97+
98+
await tasks.delete("a");
99+
})();

0 commit comments

Comments
 (0)