|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el concepto de "logging" en tu lenguaje. Configúralo y muestra |
| 4 | + * un ejemplo con cada nivel de "severidad" disponible. |
| 5 | + * |
| 6 | + * DIFICULTAD EXTRA (opcional): |
| 7 | + * Crea un programa ficticio de gestión de tareas que permita añadir, eliminar |
| 8 | + * y listar dichas tareas. |
| 9 | + * - Añadir: recibe nombre y descripción. |
| 10 | + * - Eliminar: por nombre de la tarea. |
| 11 | + * Implementa diferentes mensajes de log que muestren información según la |
| 12 | + * tarea ejecutada (a tu elección). |
| 13 | + * Utiliza el log para visualizar el tiempo de ejecución de cada tarea. |
| 14 | + */ |
| 15 | + |
| 16 | + |
| 17 | +// instalar la biblioteca de logging 'winston' |
| 18 | + |
| 19 | +const winston = require('winston') |
| 20 | + |
| 21 | +const logger1 = winston.createLogger({ |
| 22 | + level: 'debug', |
| 23 | + format: winston.format.json(), |
| 24 | + transports: [ |
| 25 | + new winston.transports.Console(), |
| 26 | + new winston.transports.File({filename: 'app.log'}), |
| 27 | + ] |
| 28 | +}) |
| 29 | + |
| 30 | +logger1.info("An info log") |
| 31 | +logger1.error("An error log") |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +/////////////// --------------------------------- EXTRA ---------------------------------- /////////////////// |
| 36 | + |
| 37 | +const readline = require('readline'); |
| 38 | +const winston = require('winston'); |
| 39 | +const { format } = require('path'); |
| 40 | + |
| 41 | +const rl = readline.createInterface({ |
| 42 | + input: process.stdin, |
| 43 | + output: process.stdout |
| 44 | +}); |
| 45 | + |
| 46 | +// configuracion del logger con winston |
| 47 | + |
| 48 | +const logger = winston.createLogger({ |
| 49 | + level: 'debug', |
| 50 | + format: winston.format.combine( |
| 51 | + winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss' }), |
| 52 | + winston.format.printf(({ timestamp, level, message}) => { |
| 53 | + return `${timestamp} - ${level.toUpperCase()} - ${message}`; |
| 54 | + }) |
| 55 | + ), |
| 56 | + transports: [ |
| 57 | + new winston.transports.Console(), |
| 58 | + new winston.transports.File({ filename: 'test.log' }) |
| 59 | + ] |
| 60 | +}); |
| 61 | + |
| 62 | + |
| 63 | +class TaskManager { |
| 64 | + constructor() { |
| 65 | + this.tasks = {}; |
| 66 | + logger.info("Gestor de Tareas inicializado."); |
| 67 | + } |
| 68 | + |
| 69 | + addTask() { |
| 70 | + rl.question("Ingrese el nombre de la tarea: ", name => { |
| 71 | + rl.question("Ingrese la descripción de la tarea: ", description => { |
| 72 | + if (this.tasks[name]) { |
| 73 | + logger.warn(`Intento de añadir una tarea ya existente: ${name}`); |
| 74 | + return; |
| 75 | + } |
| 76 | + const execution_time = Math.floor(Math.random() * 5) + 1; |
| 77 | + this.tasks[name] = { |
| 78 | + description, |
| 79 | + execution_time |
| 80 | + }; |
| 81 | + logger.info(`Tarea '${name}' añadida.`); |
| 82 | + logger.debug(`Tiempo de ejecución de la tarea '${name}': ${execution_time} segundos.`); |
| 83 | + }); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + deleteTask() { |
| 88 | + rl.question("Ingrese el nombre de la tarea a eliminar: ", name => { |
| 89 | + if (!this.tasks[name]) { |
| 90 | + logger.warn(`Intento de eliminar una tarea inexistente: ${name}`); |
| 91 | + return; |
| 92 | + } |
| 93 | + delete this.tasks[name]; |
| 94 | + logger.info(`Tarea '${name}' eliminada.`); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + listTasks() { |
| 99 | + if (Object.keys(this.tasks).length === 0) { |
| 100 | + logger.info("No hay tareas para listar."); |
| 101 | + return; |
| 102 | + } |
| 103 | + for (const [name, info] of Object.entries(this.tasks)) { |
| 104 | + console.log(`Tarea: ${name} - Descripción: ${info.description} - Tiempo de ejecución: ${info.execution_time} segundos`); |
| 105 | + logger.info(`Tiempo de ejecución de la tarea '${name}' es ${info.execution_time} segundos.`); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +const manager = new TaskManager(); |
| 112 | + |
| 113 | +function mainMenu(){ |
| 114 | + rl.question("\nGestor de Tareas\n1. Añadir Tarea\n2. Eliminar Tarea\n3. Listar Tareas\n4. Salir\n Seleccione una opcion: ", choice => { |
| 115 | + if (choice === '1'){ |
| 116 | + manager.addTask(); |
| 117 | + }else if (choice === '2'){ |
| 118 | + manager.deleteTask(); |
| 119 | + }else if (choice === '3'){ |
| 120 | + manager.listTasks(); |
| 121 | + }else if (choice === '4'){ |
| 122 | + rl.close(); |
| 123 | + return; |
| 124 | + }else{ |
| 125 | + logger.error("Opción no válida, por favor intente nuevamente."); |
| 126 | + } |
| 127 | + mainMenu(); |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +mainMenu(); |
0 commit comments