Skip to content

Commit

Permalink
feat: implement logger.info instead of console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakkas006 committed Sep 20, 2023
1 parent 07afd9b commit 7703016
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 67 deletions.
3 changes: 2 additions & 1 deletion server/scripts/seed-data/role.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { faker } = require('@faker-js/faker');
const { MongoManager } = require('../../src/modules/db/mongo');
const logger = require('../../src/logger');

const getFakeRolesData = (x) => {
const roles = []
Expand Down Expand Up @@ -27,7 +28,7 @@ const seedData = async () => {
let i = 0;
for await (const role of roles) {
const result = await insert(role);
console.log(`${index} - ${i++}`, '\t', result.insertedId);
logger.info(`${index} - ${i++}`, '\t', result.insertedId);
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions server/scripts/seed-data/video.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { faker } = require('@faker-js/faker');
const { MongoManager } = require('../../src/modules/db/mongo');
const { VIDEO_STATUS, VIDEO_VISIBILITIES } = require('../../src/modules/db/constant');
const logger = require('../../src/logger');

const getFakeVideosData = (x) => {
const videos = [];
console.log('getting new batch', x);
logger.info('getting new batch', x);
for (let i = 0; i < 1000; i++) {
videos.push({
title: faker.lorem.sentence(5),
Expand Down Expand Up @@ -44,7 +45,7 @@ const seedData = async () => {
let i = 0;
for await (const video of videos) {
const result = await insert(video);
console.log(`${index} - ${i++}`, '\t', result.insertedId);
logger.info(`${index} - ${i++}`, '\t', result.insertedId);
}
}
};
Expand Down
15 changes: 8 additions & 7 deletions server/src/modules/db/collections.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ObjectId } = require('mongodb');
const { MongoManager } = require('./mongo');
const { baseDefaults } = require('./schemas/common');
const logger = require('../../logger');

const insertItem = async (collection, item) => {
try {
Expand All @@ -9,10 +10,10 @@ const insertItem = async (collection, item) => {
...item,
});
} catch (error) {
console.error(error.errInfo?.details);
logger.error(error.errInfo?.details);
if (error.code.toString() === '121') {
// MongoServerError: Document failed validation
console.log('schemaErrors', JSON.stringify(error.errInfo?.details));
logger.info('schemaErrors', JSON.stringify(error.errInfo?.details));
}
return error;
}
Expand All @@ -31,7 +32,7 @@ const updateItem = async (collection, item) => {
}
);
} catch (error) {
console.error(error);
logger.error(error);
return null;
}
};
Expand All @@ -45,7 +46,7 @@ const getObjectById = async (collectionName, id) => {
);
return item;
} catch (error) {
console.error(error);
logger.error(error);
return error;
}
};
Expand All @@ -55,7 +56,7 @@ const search = async (
{ filter, projection, sort, pageNumber = 1, limit = 10 }
) => {
const skip = (pageNumber - 1) * limit;
console.log(
logger.info(
'search filter, projection, sort, pageNumber',
collectionName,
filter,
Expand All @@ -78,7 +79,7 @@ const count = async (collectionName, { filter }) => {
const countDocuments = filter
? await cursor.countDocuments(filter)
: await cursor.estimatedDocumentCount();
console.log('count:', collectionName, filter, countDocuments);
logger.info('count:', collectionName, filter, countDocuments);
return countDocuments;
};

Expand All @@ -92,7 +93,7 @@ const deleteObjectById = async (collectionName, id) => {

return result;
} catch (error) {
console.error(error);
logger.error(error);
return error;
}
};
Expand Down
4 changes: 2 additions & 2 deletions server/src/modules/db/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const logger = require('../../logger');
class MongoManager {
static async setInstance(instance) {
if (!MongoManager.instance) {
console.log('setting instance');
logger.info('setting instance');
MongoManager.instance = instance;
}
}
Expand All @@ -38,7 +38,7 @@ class MongoManager {
const client = new MongoClient(mongoUrl, {
useNewUrlParser: true,
});
console.log('connecting to MongoDB');
logger.info('connecting to MongoDB');
await client.connect();
const db = client.db('videodb');
if (process.env.ENABLE_WINSTON_MONGODB === 'true') {
Expand Down
5 changes: 3 additions & 2 deletions server/src/modules/db/schemas/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ObjectId } = require('mongodb');
const logger = require('../../../logger');

/**
* Common properties for all of the collections
Expand Down Expand Up @@ -51,10 +52,10 @@ const baseDefaults = () => ({
const ensureCollection = async ({ db, name, validator, indexes }) => {
const collections = await db.listCollections({ name }).toArray();
if (collections.length === 0) {
console.log(`creating collection ${name}`);
logger.info(`creating collection ${name}`);
await db.createCollection(name, { validator });
} else {
console.log(`updating collection ${name}`);
logger.info(`updating collection ${name}`);
await db.command({
collMod: name,
validator,
Expand Down
7 changes: 4 additions & 3 deletions server/src/modules/models/role/controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const logger = require('../../../logger');
const { insert, update, getById } = require('./service');

const BASE_URL = `/api/roles`;

const setupRoutes = (app) => {
console.log(`Setting up routes for ${BASE_URL}`);
logger.info(`Setting up routes for ${BASE_URL}`);

app.post(`${BASE_URL}/create`, async (req, res) => {
console.log('rolecreate create', req.body);
logger.info('rolecreate create', req.body);
const result = await insert(req.body);
if (result instanceof Error) {
res.status(400).json(JSON.parse(result.message));
Expand All @@ -25,7 +26,7 @@ const setupRoutes = (app) => {
});

app.get(`${BASE_URL}/detail/:id`, async (req, res) => {
console.log(`GET`, req.params);
logger.info(`GET`, req.params);
const item = await getById(req.params.id);
if (item instanceof Error) {
return res.status(400).json(JSON.parse(item.message));
Expand Down
3 changes: 2 additions & 1 deletion server/src/modules/models/role/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ObjectId } = require('mongodb');
const { MongoManager } = require('../../db/mongo');
const { Role } = require('../../db/collections');
const logger = require('../../../logger');

const insert = async (role) => {
try {
Expand All @@ -15,7 +16,7 @@ const update = async (role) => {
try {
return await Role.update(role);
} catch (error) {
console.error(error);
logger.error(error);
return error;
}
};
Expand Down
25 changes: 13 additions & 12 deletions server/src/modules/models/video/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ const { addQueueItem } = require('../../queues/queue');
const {
getVideoDurationAndResolution,
} = require('../../queues/video-processor');
const logger = require('../../../logger');

const BASE_URL = `/api/videos`;

const setupRoutes = (app) => {
console.log(`Setting up routes for ${BASE_URL}`);
logger.info(`Setting up routes for ${BASE_URL}`);

// return empty response with success message for the base route
app.get(`${BASE_URL}/`, async (req, res) => {
console.log(`GET`, req.params);
logger.info(`GET`, req.params);
const data = await search({});
res.send({
status: 'success',
Expand All @@ -32,7 +33,7 @@ const setupRoutes = (app) => {
});

app.get(`${BASE_URL}/detail/:id`, async (req, res) => {
console.log(`GET`, req.params);
logger.info(`GET`, req.params);
const video = await updateViewCount(req.params.id);
if (video instanceof Error) {
return res.status(400).json(JSON.parse(video.message));
Expand All @@ -42,13 +43,13 @@ const setupRoutes = (app) => {

// TODO: Proper searching with paging and ordering
app.post(`${BASE_URL}/search`, async (req, res) => {
console.log('POST search', req.body);
logger.info('POST search', req.body);
const result = await search(req.body);
res.send(result);
});

app.post(`${BASE_URL}/count`, async (req, res) => {
console.log('POST count', req.body);
logger.info('POST count', req.body);
const result = await count(req.body);
res.send({count: result});
});
Expand Down Expand Up @@ -84,7 +85,7 @@ const setupRoutes = (app) => {
});

app.delete(`${BASE_URL}/delete/:id`, async (req, res) => {
console.log('DELETE', req.params.id);
logger.info('DELETE', req.params.id);
if (req.params.id) {
const result = await deleteById(req.params.id);
if (result instanceof Error) {
Expand All @@ -110,10 +111,10 @@ const setupRoutes = (app) => {

const fileFilter = (req, file, cb) => {
if (file.mimetype === 'video/mp4' || file.mimetype === 'video/webm') {
console.log('file type supported', file);
logger.info('file type supported', file);
cb(null, true);
} else {
console.log('file type not supported', file);
logger.info('file type not supported', file);
cb(new multer.MulterError('File type not supported'), false);
}
};
Expand All @@ -132,7 +133,7 @@ const setupRoutes = (app) => {
res.status(400).json({ status: 'error', error: err });
return;
} else {
console.log('upload success', req.file);
logger.info('upload success', req.file);
// res.status(200).json({ status: "success", message: "upload success" });
next();
}
Expand All @@ -155,10 +156,10 @@ const setupRoutes = (app) => {
viewCount: 0,
duration: 0,
};
console.log('dbPayload', dbPayload);
logger.info('dbPayload', dbPayload);
// TODO: save the file info and get the id from the database
const result = await insert(dbPayload);
console.log('result', result);
logger.info('result', result);
await addQueueItem(QUEUE_EVENTS.VIDEO_UPLOADED, {
id: result.insertedId.toString(),
...req.body,
Expand All @@ -172,7 +173,7 @@ const setupRoutes = (app) => {
});
return;
} catch (error) {
console.error(error);
logger.error(error);
res.send(error);
}
});
Expand Down
7 changes: 4 additions & 3 deletions server/src/modules/models/video/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ObjectId } = require('mongodb');
const { Video } = require('../../db/collections');
const { VIDEO_STATUS } = require('../../db/constant');
const logger = require('../../../logger');

const insert = async (document) => {
try {
Expand All @@ -19,7 +20,7 @@ const update = async (document) => {
};

const search = async (searchObject) => {
console.log('searchObject', searchObject);
logger.info('searchObject', searchObject);
const filter = searchObject.filterKey
? {
[searchObject.filterKey]: new RegExp(searchObject.filterValue, 'i'),
Expand Down Expand Up @@ -59,7 +60,7 @@ const search = async (searchObject) => {
};

const count = async (searchObject) => {
console.log('searchObject', searchObject);
logger.info('searchObject', searchObject);
const filter = searchObject.filterKey
? {
[searchObject.filterKey]: new RegExp(searchObject.filterValue, 'i'),
Expand All @@ -82,7 +83,7 @@ const updateHistory = async (id, { history, ...rest }) => {
);
return updatedDoc;
} catch (error) {
console.error(error);
logger.error(error);
return error;
}
};
Expand Down
15 changes: 8 additions & 7 deletions server/src/modules/queues/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const {
generateThumbnail,
} = require('./video-processor');
const { addQueueItem } = require('./queue');
const logger = require('../../logger');
const eventEmitter = require('../../event-manager').getInstance();

const uploadedHandler = async (job) => {
console.log('uploaded handler!', job.data.title);
logger.info('uploaded handler!', job.data.title);
await addQueueItem(QUEUE_EVENTS.VIDEO_PROCESSING, {
...job.data,
completed: true,
Expand All @@ -20,7 +21,7 @@ const uploadedHandler = async (job) => {
};

const processingHandler = async (job) => {
console.log('processing handler!', job.data.path);
logger.info('processing handler!', job.data.path);
await processRawFileToMp4(`./${job.data.path}`, `./uploads/processed`, {
...job.data,
completed: true,
Expand All @@ -30,7 +31,7 @@ const processingHandler = async (job) => {
};

const processedHandler = async (job) => {
console.log('processed handler!', job.data.path);
logger.info('processed handler!', job.data.path);
await addQueueItem(QUEUE_EVENTS.VIDEO_HLS_CONVERTING, {
...job.data,
completed: true,
Expand All @@ -40,7 +41,7 @@ const processedHandler = async (job) => {
};

const hlsConvertingHandler = async (job) => {
console.log('HLS converting handler!', job.data.path);
logger.info('HLS converting handler!', job.data.path);
const hlsConverted = await processMp4ToHls(
`./${job.data.path}`,
`./uploads/hls`,
Expand All @@ -50,12 +51,12 @@ const hlsConvertingHandler = async (job) => {
next: QUEUE_EVENTS.VIDEO_HLS_CONVERTED,
}
);
console.log('hlsConverted', hlsConverted);
logger.info('hlsConverted', hlsConverted);
return;
};

const hlsConvertedHandler = async (job) => {
console.log('hls converted handler!', job.data.filename);
logger.info('hls converted handler!', job.data.filename);
await addQueueItem(NOTIFY_EVENTS.NOTIFY_VIDEO_HLS_CONVERTED, {
...job.data,
completed: true,
Expand All @@ -65,7 +66,7 @@ const hlsConvertedHandler = async (job) => {
};

const notifyVideoHlsConvertedHandler = async (job) => {
console.log('notifyVideoHlsConvertedHandler handler!', job.data);
logger.info('notifyVideoHlsConvertedHandler handler!', job.data);
eventEmitter.emit(`${NOTIFY_EVENTS.NOTIFY_VIDEO_HLS_CONVERTED}`, job.data);
return { ...job.data, completed: true, next: null };
};
Expand Down
3 changes: 2 additions & 1 deletion server/src/modules/queues/queue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Queue } = require('bullmq');
const { ALL_EVENTS: QUEUE_EVENTS } = require('./constants');
const logger = require('../../logger');
const eventEmitter = require('../../event-manager').getInstance();

const redisConnection = {
Expand All @@ -15,7 +16,7 @@ const queues = Object.values(QUEUE_EVENTS).map((queueName) => {
});

const addQueueItem = async (queueName, item) => {
console.log('addQueueItem', queueName, item);
logger.info('addQueueItem', queueName, item);
const queue = queues.find((q) => q.name === queueName);
if (!queue) {
throw new Error(`queue ${queueName} not found`);
Expand Down
Loading

0 comments on commit 7703016

Please sign in to comment.