From 138d230275841e049d8dea9e4142a2349a18e17f Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 26 Jan 2024 08:13:25 +0000 Subject: [PATCH 1/2] fix: use logger instead of console --- .../jobs/fallback/jobs-event.service.ts | 18 +++++++----------- .../jobs/jobs/at-import/helpers/EntityMap.ts | 17 ++++++++++------- .../jobs/jobs/at-import/helpers/fetchAT.ts | 13 ++++++++----- .../modules/jobs/redis/jobs-event.service.ts | 12 ++---------- .../modules/jobs/redis/jobs-redis.service.ts | 6 ++++-- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/packages/nocodb/src/modules/jobs/fallback/jobs-event.service.ts b/packages/nocodb/src/modules/jobs/fallback/jobs-event.service.ts index 207f3becf1..9abbb7ae37 100644 --- a/packages/nocodb/src/modules/jobs/fallback/jobs-event.service.ts +++ b/packages/nocodb/src/modules/jobs/fallback/jobs-event.service.ts @@ -5,12 +5,14 @@ import { Processor, } from '@nestjs/bull'; import { Job } from 'bull'; -import boxen from 'boxen'; import { EventEmitter2 } from '@nestjs/event-emitter'; +import { Logger } from '@nestjs/common'; import { JobEvents, JOBS_QUEUE, JobStatus } from '~/interface/Jobs'; @Processor(JOBS_QUEUE) export class JobsEventService { + protected logger = new Logger(JobsEventService.name); + constructor(private eventEmitter: EventEmitter2) {} @OnQueueActive() @@ -23,18 +25,12 @@ export class JobsEventService { @OnQueueFailed() onFailed(job: Job, error: Error) { - console.error( - boxen( - `---- !! JOB FAILED !! ----\nid:${job.id}\nerror:${error.name} (${error.message})\n\nstack: ${error.stack}`, - { - padding: 1, - borderStyle: 'double', - borderColor: 'yellow', - }, - ), + this.logger.error( + `---- !! JOB FAILED !! ----\nid:${job.id}\nerror:${error.name} (${error.message})\n\nstack: ${error.stack}`, ); - this.eventEmitter.emit(JobEvents.STATUS, { + const newLocal = this; + newLocal.eventEmitter.emit(JobEvents.STATUS, { id: job.id.toString(), status: JobStatus.FAILED, data: { diff --git a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/EntityMap.ts b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/EntityMap.ts index afbd1f0f53..46c3c69ea4 100644 --- a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/EntityMap.ts +++ b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/EntityMap.ts @@ -1,5 +1,8 @@ import { Readable } from 'stream'; import sqlite3 from 'sqlite3'; +import { Logger } from '@nestjs/common'; + +const logger = new Logger('EntityMap'); class EntityMap { initialized: boolean; @@ -18,7 +21,7 @@ class EntityMap { : 'mappingPlaceholder TEXT'; db.run(`CREATE TABLE mapping (${colStatement})`, (err) => { if (err) { - console.log(err); + logger.log(err); reject(err); } resolve(db); @@ -55,7 +58,7 @@ class EntityMap { new Promise((resolve, reject) => { this.db.run(`ALTER TABLE mapping ADD '${col}' TEXT;`, (err) => { if (err) { - console.log(err); + logger.log(err); reject(err); } this.cols.push(col); @@ -80,7 +83,7 @@ class EntityMap { values, (err) => { if (err) { - console.log(err); + logger.log(err); reject(err); } resolve(true); @@ -103,7 +106,7 @@ class EntityMap { [val], (err, rs) => { if (err) { - console.log(err); + logger.log(err); reject(err); } if (rs) { @@ -122,7 +125,7 @@ class EntityMap { return new Promise((resolve, reject) => { this.db.get(`SELECT COUNT(*) as count FROM mapping`, (err, rs) => { if (err) { - console.log(err); + logger.log(err); reject(err); } resolve(rs.count); @@ -153,7 +156,7 @@ class EntityMap { } FROM mapping LIMIT ${limit} OFFSET ${offset}`, (err, rs) => { if (err) { - console.log(err); + logger.log(err); reject(err); } for (let row of rs) { @@ -200,7 +203,7 @@ function processResponseRow(res: any) { try { res[key] = JSON.parse(res[key].replace('JSON::', '')); } catch (e) { - console.log(e); + logger.log(e); } } if (revertKey(key) !== key) { diff --git a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/fetchAT.ts b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/fetchAT.ts index e6144eca0c..7da9c4d827 100644 --- a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/fetchAT.ts +++ b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/fetchAT.ts @@ -1,5 +1,8 @@ +import { Logger } from '@nestjs/common'; import axios from 'axios'; +const logger = new Logger('FetchAT'); + const info: any = { initialized: false, }; @@ -44,7 +47,7 @@ async function initialize(shareId, appId?: string) { return response.data; }) .catch((e) => { - console.log(e); + logger.log(e); throw { message: 'Invalid Shared Base ID :: Ensure www.airtable.com/ is accessible. Refer https://bit.ly/3x0OdXI for details', @@ -94,7 +97,7 @@ async function initialize(shareId, appId?: string) { info.baseId = info.baseInfo.applicationId; info.initialized = true; } catch (e) { - console.log(e); + logger.log(e); info.initialized = false; if (e.message) { throw e; @@ -135,7 +138,7 @@ async function read() { return response.data; }) .catch((e) => { - console.log(e); + logger.log(e); throw { message: 'Error Reading :: Ensure www.airtable.com/ is accessible. Refer https://bit.ly/3x0OdXI for details', @@ -202,7 +205,7 @@ async function readView(viewId) { return response.data; }) .catch((e) => { - console.log(e); + logger.log(e); throw { message: 'Error Reading View :: Ensure www.airtable.com/ is accessible. Refer https://bit.ly/3x0OdXI for details', @@ -252,7 +255,7 @@ async function readTemplate(templateId) { return response.data; }) .catch((e) => { - console.log(e); + logger.log(e); throw { message: 'Error Fetching :: Ensure www.airtable.com/templates/featured/ is accessible.', diff --git a/packages/nocodb/src/modules/jobs/redis/jobs-event.service.ts b/packages/nocodb/src/modules/jobs/redis/jobs-event.service.ts index 99dfb9f34b..0382a0771b 100644 --- a/packages/nocodb/src/modules/jobs/redis/jobs-event.service.ts +++ b/packages/nocodb/src/modules/jobs/redis/jobs-event.service.ts @@ -5,7 +5,6 @@ import { Processor, } from '@nestjs/bull'; import { Job } from 'bull'; -import boxen from 'boxen'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Logger } from '@nestjs/common'; import { JobEvents, JOBS_QUEUE, JobStatus } from '~/interface/Jobs'; @@ -28,15 +27,8 @@ export class JobsEventService { @OnQueueFailed() onFailed(job: Job, error: Error) { this.logger.error(`Job ${job.id} failed with error ${error.message}`); - console.error( - boxen( - `---- !! JOB FAILED !! ----\nid:${job.id}\nerror:${error.name} (${error.message})\n\nstack: ${error.stack}`, - { - padding: 1, - borderStyle: 'double', - borderColor: 'yellow', - }, - ), + this.logger.error( + `---- !! JOB FAILED !! ----\nid:${job.id}\nerror:${error.name} (${error.message})\n\nstack: ${error.stack}`, ); this.eventEmitter.emit(JobEvents.STATUS, { diff --git a/packages/nocodb/src/modules/jobs/redis/jobs-redis.service.ts b/packages/nocodb/src/modules/jobs/redis/jobs-redis.service.ts index d431865f77..67af1d76eb 100644 --- a/packages/nocodb/src/modules/jobs/redis/jobs-redis.service.ts +++ b/packages/nocodb/src/modules/jobs/redis/jobs-redis.service.ts @@ -1,9 +1,11 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import Redis from 'ioredis'; import { InstanceTypes } from '~/interface/Jobs'; @Injectable() export class JobsRedisService { + protected logger = new Logger(JobsRedisService.name); + private redisClient: Redis; private redisSubscriber: Redis; private unsubscribeCallbacks: { [key: string]: () => void } = {}; @@ -42,7 +44,7 @@ export class JobsRedisService { try { this.redisClient.publish(channel, JSON.stringify(message)); } catch (e) { - console.error(e); + this.logger.error(e); } } } From 2e26c8acc5fb5ca90bbdf114988b7d37a2d91424 Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 26 Jan 2024 08:13:25 +0000 Subject: [PATCH 2/2] fix: log errors to instance --- .../modules/jobs/jobs/at-import/helpers/readAndProcessData.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/readAndProcessData.ts b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/readAndProcessData.ts index 4e697ac531..03655dbb4f 100644 --- a/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/readAndProcessData.ts +++ b/packages/nocodb/src/modules/jobs/jobs/at-import/helpers/readAndProcessData.ts @@ -166,6 +166,7 @@ export async function importData({ logDetailed, logWarning, }).catch((e) => { + logger.error(e); logWarning(`There were errors on reading '${table.title}' data :: ${e}`); }); @@ -187,6 +188,7 @@ export async function importData({ logDetailed, logWarning, }).catch((e) => { + logger.error(e); logWarning( `There were errors on importing '${table.title}' LTAR data :: ${e}`, );