From e6e5e4a614be693f1a0e9aa67466e68ebd476fb1 Mon Sep 17 00:00:00 2001 From: Onja Date: Tue, 10 Oct 2023 16:21:39 +0300 Subject: [PATCH] Refactor consoleSubscriber.js to use a log function to log event names and data --- src/subscribers/consoleSubscriber.js | 30 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/subscribers/consoleSubscriber.js b/src/subscribers/consoleSubscriber.js index 930de63..64c5aa6 100644 --- a/src/subscribers/consoleSubscriber.js +++ b/src/subscribers/consoleSubscriber.js @@ -1,23 +1,31 @@ +const { text } = require('express'); const emitter = require('../services/eventEmitter'); +// Create a log function that logs the event name and the data +const log = (event, data) => { + console.log(`----- Event: ${event} ----- `); + console.log(data); + console.log('-----'); +}; + // Create a new listener for the download.start event emitter.on('download.start', ({ url, filepath }) => { - console.log(`Downloading ${url} to ${filepath}`); + log('download.start', `Downloading ${url} to ${filepath}`); }); // Create a new listener for the download.end event emitter.on('download.end', ({ url, filepath }) => { - console.log(`Downloaded ${url} to ${filepath}`); + log('download.end', `Downloaded ${url} to ${filepath}`); }); // Create a new listener for the download.error event emitter.on('download.error', ({ url, filepath, error, type }) => { - console.error(`Error [${type}] downloading ${url} to ${filepath}: ${error}`); + log('download.error', `Error [${type}] downloading ${url} to ${filepath}: ${error}`); }); // Create a new listener for the download.progress event emitter.on('download.progress', ({ url, filepath, percentage }) => { - console.log(`Downloaded ${percentage}% of ${url} to ${filepath}`); + log('download.progress', `Downloaded ${percentage}% of ${url} to ${filepath}`); }); @@ -26,17 +34,17 @@ emitter.on('download.progress', ({ url, filepath, percentage }) => { // Create a new listener for the parseFromUrl.start event emitter.on('parseFromUrl.start', ({ url, columns }) => { - console.log(`Parsing ${url} with columns [${columns.join(', ')}]`); + log('parseFromUrl.start', `Parsing ${url} with columns [${columns.join(', ')}]`); }); // Create a new listener for the parseFromUrl.end event emitter.on('parseFromUrl.end', ({ url, columns, filepath }) => { - console.log(`Parsed ${url} with columns ${columns.join(', ')} to ${filepath}`); + log('parseFromUrl.end', `Parsed ${url} with columns ${columns.join(', ')} to ${filepath}`); }); // Create a new listener for the parseFromUrl.error event emitter.on('parseFromUrl.error', ({ url, columns, error }) => { - console.error(`Error parsing ${url} with columns ${columns.join(', ')}: ${error}`); + log('parseFromUrl.error', `Error parsing ${url} with columns ${columns.join(', ')}: ${error}`); }); @@ -45,20 +53,20 @@ emitter.on('parseFromUrl.error', ({ url, columns, error }) => { // Create a new listener for the parse start event emitter.on('parse.start', ({ filepath, columns, headers }) => { - console.log(`Parsing ${filepath} with columns ${columns.join(', ')} and headers ${headers.join(', ')}`); + log('parse.start', `Parsing ${filepath} with columns ${columns.join(', ')} and headers ${headers.join(', ')}`); }); // Create a new listener for the parse.end event emitter.on('parse.end', ({ filepath, columns, count }) => { - console.log(`Parsed ${filepath} with columns ${columns.join(', ')} and ${count} rows`); + log('parse.end', `Parsed ${filepath} with columns ${columns.join(', ')} and ${count} rows`); }); // Create a new listener for the parse.error event emitter.on('parse.error', ({ filepath, columns, error }) => { - console.error(`Error parsing ${filepath} with columns ${columns.join(', ')}: ${error}`); + log('parse.error', `Error parsing ${filepath} with columns ${columns.join(', ')}: ${error}`); }); // Create a new listener for the parse.data event emitter.on('parse.data', ({ filepath, columns, data, index }) => { - console.log(`Parsed ${filepath} with columns ${columns.join(', ')} at index ${index}`); + log('parse.data', `Parsed ${filepath} with columns ${columns.join(', ')} at index ${index}`); }); \ No newline at end of file