From 19ea961c36ce5e1ef0906c5c2b00f58873902b88 Mon Sep 17 00:00:00 2001 From: Dmitryii Osipov Date: Tue, 22 Aug 2023 17:05:43 +0400 Subject: [PATCH] Improve backend logging (#1354) --- .gitignore | 3 +- idea/api-gateway/src/common/logger.ts | 5 --- .../src/{config/index.ts => config.ts} | 0 idea/api-gateway/src/main.ts | 7 +-- .../src/{rabbitmq/index.ts => rmq.ts} | 24 ++++------ idea/api-gateway/src/server.ts | 24 +++++----- idea/common/src/decorators/form-response.ts | 15 +++++-- idea/common/src/logger.ts | 15 +++++++ idea/common/src/logger/index.ts | 24 ---------- idea/indexer/src/common/index.ts | 1 - idea/indexer/src/common/logger.ts | 3 -- idea/indexer/src/gear/connect.ts | 12 ++--- idea/indexer/src/gear/indexer.ts | 44 +++++++++---------- idea/indexer/src/healthcheck.server.ts | 2 +- idea/indexer/src/main.ts | 4 +- idea/indexer/src/one-time-sync.ts | 8 ++-- idea/indexer/src/rmq.ts | 14 +++--- idea/indexer/src/services/message.service.ts | 6 +-- idea/indexer/src/services/program.service.ts | 4 +- idea/meta-storage/src/logger.ts | 3 -- idea/meta-storage/src/main.ts | 3 +- idea/meta-storage/src/rmq.ts | 8 ++-- idea/test-balance/src/common/logger.ts | 5 --- .../src/common/transfer-balance-process.ts | 14 +++--- idea/test-balance/src/common/types.ts | 4 -- idea/test-balance/src/gear/connection.ts | 8 ++-- idea/test-balance/src/gear/index.ts | 15 ++++--- idea/test-balance/src/gear/types.ts | 7 --- .../src/{routes => }/healthcheck.router.ts | 0 idea/test-balance/src/main.ts | 7 +-- idea/test-balance/src/rabbitmq/consumer.ts | 7 ++- idea/test-balance/src/rabbitmq/producer.ts | 2 +- .../src/rabbitmq/{init-rabbitmq.ts => rmq.ts} | 19 ++++---- idea/test-balance/src/rabbitmq/types/index.ts | 8 ---- .../src/{services => }/transfer.service.ts | 4 +- 35 files changed, 144 insertions(+), 185 deletions(-) delete mode 100644 idea/api-gateway/src/common/logger.ts rename idea/api-gateway/src/{config/index.ts => config.ts} (100%) rename idea/api-gateway/src/{rabbitmq/index.ts => rmq.ts} (85%) create mode 100644 idea/common/src/logger.ts delete mode 100644 idea/common/src/logger/index.ts delete mode 100644 idea/indexer/src/common/logger.ts delete mode 100644 idea/meta-storage/src/logger.ts delete mode 100644 idea/test-balance/src/common/logger.ts delete mode 100644 idea/test-balance/src/common/types.ts delete mode 100644 idea/test-balance/src/gear/types.ts rename idea/test-balance/src/{routes => }/healthcheck.router.ts (100%) rename idea/test-balance/src/rabbitmq/{init-rabbitmq.ts => rmq.ts} (84%) delete mode 100644 idea/test-balance/src/rabbitmq/types/index.ts rename idea/test-balance/src/{services => }/transfer.service.ts (86%) diff --git a/.gitignore b/.gitignore index 18f1ac3e70..f90c995873 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ node_modules/ !.yarn/sdks !.yarn/versions **/dist/ -.vscode/ \ No newline at end of file +.vscode/ +idea/tmp/ diff --git a/idea/api-gateway/src/common/logger.ts b/idea/api-gateway/src/common/logger.ts deleted file mode 100644 index dd032bc9d5..0000000000 --- a/idea/api-gateway/src/common/logger.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getLogger } from '@gear-js/common'; - -const API_GATEWAY = process.env.NODE_ENV === 'dev' ? 'API_GATEWAY' : ''; - -export const logger = getLogger(API_GATEWAY); diff --git a/idea/api-gateway/src/config/index.ts b/idea/api-gateway/src/config.ts similarity index 100% rename from idea/api-gateway/src/config/index.ts rename to idea/api-gateway/src/config.ts diff --git a/idea/api-gateway/src/main.ts b/idea/api-gateway/src/main.ts index 892d72a428..9f56f1ba46 100644 --- a/idea/api-gateway/src/main.ts +++ b/idea/api-gateway/src/main.ts @@ -1,12 +1,13 @@ -import { logger } from './common/logger'; -import { RMQService } from './rabbitmq'; +import { logger } from '@gear-js/common'; + +import { RMQService } from './rmq'; import { Server, changeStatus } from './server'; const bootstrap = async () => { const rmq = new RMQService(); await rmq.init(); - logger.info('Connected to RabbitMQ'); + logger.info('RabbitMQ connection established sucessfuly'); changeStatus(); diff --git a/idea/api-gateway/src/rabbitmq/index.ts b/idea/api-gateway/src/rmq.ts similarity index 85% rename from idea/api-gateway/src/rabbitmq/index.ts rename to idea/api-gateway/src/rmq.ts index f3b301a394..441fb2cb91 100644 --- a/idea/api-gateway/src/rabbitmq/index.ts +++ b/idea/api-gateway/src/rmq.ts @@ -1,9 +1,8 @@ import { CronJob } from 'cron'; import { connect, Connection, Channel } from 'amqplib'; -import { RMQExchanges, RMQMessage, RMQQueues, RMQReply, RMQServiceActions, RMQServices } from '@gear-js/common'; +import { logger, RMQExchanges, RMQMessage, RMQQueues, RMQReply, RMQServiceActions, RMQServices } from '@gear-js/common'; -import config from '../config'; -import { logger } from '../common/logger'; +import config from './config'; export class RMQService { private indexerChannels: Map; @@ -22,9 +21,8 @@ export class RMQService { public async init(): Promise { try { this.connection = await connect(config.rabbitmq.url); - } catch (err) { - logger.error('Failed to connecto to RabbitMQ'); - console.log(err); + } catch (error) { + logger.error('Failed to connect to to RabbitMQ', { error }); process.exit(1); } @@ -98,8 +96,7 @@ export class RMQService { autoDelete: true, }); - logger.info(`Indexer: Add new genesis ${genesis}`); - logger.info(`Indexer genesises: ${JSON.stringify(Array.from(this.indexerChannels.keys()), undefined, 2)}`); + logger.info(`New indexer genesis: ${genesis}`, { all: Array.from(this.indexerChannels.keys()) }); } if (service === RMQServices.TEST_BALANCE) { if (this.tbChannels.has(genesis)) return; @@ -107,8 +104,7 @@ export class RMQService { const channel = await this.createChannel(); this.tbChannels.set(genesis, channel); await channel.assertQueue(`${RMQServices.TEST_BALANCE}.${genesis}`, { durable: false, exclusive: false }); - logger.info(`TB: Add new genesis ${genesis}`); - logger.info(`TB genesises: ${JSON.stringify(Array.from(this.tbChannels.keys()), undefined, 2)}`); + logger.info(`New test_balance genesis ${genesis}`, { all: Array.from(this.tbChannels.keys()) }); } } @@ -118,10 +114,7 @@ export class RMQService { if (channel) { await channel.close(); this.indexerChannels.delete(genesis); - logger.info(`Indexer: Delete genesis ${genesis}`); - logger.info( - `Indexer genesises: ${JSON.stringify(Array.from(this.indexerChannels.keys()), undefined, 2)}`, - ); + logger.info(`Remove indexer genesis ${genesis}`, { all: Array.from(this.indexerChannels.keys()) }); } } if (service === RMQServices.TEST_BALANCE) { @@ -129,8 +122,7 @@ export class RMQService { if (channel) { await channel.close(); this.tbChannels.delete(genesis); - logger.info(`TB: Delete new genesis ${genesis}`); - logger.info(`TB genesises: ${JSON.stringify(Array.from(this.tbChannels.keys()), undefined, 2)}`); + logger.info(`Remove test_balance genesis ${genesis}`, { all: Array.from(this.tbChannels.keys()) }); } } } diff --git a/idea/api-gateway/src/server.ts b/idea/api-gateway/src/server.ts index a9d641f7b6..873f159ecd 100644 --- a/idea/api-gateway/src/server.ts +++ b/idea/api-gateway/src/server.ts @@ -1,10 +1,3 @@ -import { getResponse } from './utils'; - -import express, { Express, Request, Response } from 'express'; -import { checkGenesisMiddleware, captchaMiddleware, validateJsonRpcRequestMiddleware } from './middleware'; -import { logger } from './common/logger'; -import config from './config'; -import { RMQService } from './rabbitmq'; import { INDEXER_METHODS, META_STORAGE_METHODS, @@ -15,8 +8,15 @@ import { IRpcResponse, JSONRPC_ERRORS, API_GATEWAY_METHODS, + logger, } from '@gear-js/common'; import { nanoid } from 'nanoid'; +import express, { Express, Request, Response } from 'express'; + +import { getResponse } from './utils'; +import { checkGenesisMiddleware, captchaMiddleware, validateJsonRpcRequestMiddleware } from './middleware'; +import config from './config'; +import { RMQService } from './rmq'; const status = { rmq: false, @@ -60,10 +60,8 @@ export class Server { try { const result = await this.handleRequest(req.body); res.json(result); - } catch (err) { - logger.error(`ApiGatewayRouter: ${err}`); - console.log(req.body); - console.log(err); + } catch (error) { + logger.error('Handle request error', { error, request: req.body }); } }, ); @@ -78,9 +76,7 @@ export class Server { } public run() { - return this.app.listen(config.server.port, () => - console.log(`⚙️ 🚀 App successfully run on the ${config.server.port}`), - ); + return this.app.listen(config.server.port, () => logger.info(`App successfully run on the ${config.server.port}`)); } private async handleRequest(rpcBodyRequest: IRpcRequest | IRpcRequest[]): Promise { diff --git a/idea/common/src/decorators/form-response.ts b/idea/common/src/decorators/form-response.ts index 9c560cdffe..09d59b8b2c 100644 --- a/idea/common/src/decorators/form-response.ts +++ b/idea/common/src/decorators/form-response.ts @@ -1,11 +1,20 @@ +import { JSONRPC_ERRORS } from '../jsonrpc-errors'; +import { logger } from '../logger'; + export function FormResponse(target: unknown, propertyKey: string, descriptor: TypedPropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = async function SafeWrapper() { try { return { result: await originalMethod.apply(this, arguments) }; - } catch (ex) { - console.error(ex); - return { error: ex.name }; + } catch (error) { + if (error.name) { + const { name, ...err } = error; + logger.error(name, { error: err, stack: error.stack }); + return { error: name }; + } else { + logger.error('Unknown error', { error, stack: error.stack }); + return { error: JSONRPC_ERRORS.InternalError.name }; + } } }; return descriptor; diff --git a/idea/common/src/logger.ts b/idea/common/src/logger.ts new file mode 100644 index 0000000000..6e7c9cb24b --- /dev/null +++ b/idea/common/src/logger.ts @@ -0,0 +1,15 @@ +import { createLogger, format, transports } from 'winston'; + +const TIMESTAMP_FORMAT = 'YY-MM-DD HH:mm:ss'; + +export const logger = createLogger({ + format: format.combine( + format.timestamp({ format: TIMESTAMP_FORMAT }), + format.printf(({ level, message, timestamp, ...meta }) => { + const msg = { message, ...meta }; + + return `${timestamp} [${level}] ${JSON.stringify(msg)}`; + }), + ), + transports: [new transports.Console()], +}); diff --git a/idea/common/src/logger/index.ts b/idea/common/src/logger/index.ts deleted file mode 100644 index 5027389161..0000000000 --- a/idea/common/src/logger/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { createLogger, format, transports } from 'winston'; - -const TIMESTAMP_FORMAT = 'DD/MM/YY HH:mm:ss'; - -function customFormat(serviceLabel?: string) { - const formats = [ - format.timestamp({ format: TIMESTAMP_FORMAT }), - format.printf(({ level, message, timestamp }) => { - return `[${level}] ${timestamp}: ${message}`; - }), - ]; - - if (process.env.DEV) { - formats.unshift(format.label({ label: serviceLabel ? serviceLabel : '', message: true }), format.colorize()); - } - - return format.combine(...formats); -} - -export const getLogger = (serviceLabel?: string) => - createLogger({ - format: customFormat(serviceLabel), - transports: [new transports.Console()], - }); diff --git a/idea/indexer/src/common/index.ts b/idea/indexer/src/common/index.ts index 0af310c45e..4aa19ad9b1 100644 --- a/idea/indexer/src/common/index.ts +++ b/idea/indexer/src/common/index.ts @@ -3,4 +3,3 @@ export * from './enums'; export * from './types'; export * from './errors'; export * from './helpers'; -export * from './logger'; diff --git a/idea/indexer/src/common/logger.ts b/idea/indexer/src/common/logger.ts deleted file mode 100644 index f9386d2e91..0000000000 --- a/idea/indexer/src/common/logger.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { getLogger } from '@gear-js/common'; - -export const logger = getLogger('Indexer'); diff --git a/idea/indexer/src/gear/connect.ts b/idea/indexer/src/gear/connect.ts index 15acbc7122..c760a19753 100644 --- a/idea/indexer/src/gear/connect.ts +++ b/idea/indexer/src/gear/connect.ts @@ -1,9 +1,8 @@ import { GearApi } from '@gear-js/api'; -import { RMQServiceActions } from '@gear-js/common'; +import { RMQServiceActions, logger } from '@gear-js/common'; import config from '../config'; import { changeStatus } from '../healthcheck.server'; -import { logger } from '../common'; import { GearIndexer } from './indexer'; const addresses = config.gear.providerAddresses; @@ -33,6 +32,7 @@ export async function connectToNode(indexer: GearIndexer, cb: GenesisCb) { const genesis = api.genesisHash.toHex(); api.on('disconnected', () => { + logger.warn('Disconnected from the node.'); indexer.stop(); genesis && cb(RMQServiceActions.DELETE, genesis); reconnect(api, indexer, cb); @@ -41,7 +41,7 @@ export async function connectToNode(indexer: GearIndexer, cb: GenesisCb) { reconnectionsCounter = 0; await indexer.run(api); cb(RMQServiceActions.ADD, genesis); - logger.info(`⚙️ Connected to ${api.runtimeChain} with genesis ${genesis}`); + logger.info(`Connected to ${api.runtimeChain} with genesis ${genesis}`); changeStatus('gear'); } @@ -54,8 +54,8 @@ async function reconnect(api: GearApi, indexer: GearIndexer, cb: GenesisCb) { try { await api.disconnect(); - } catch (err) { - console.log(err); + } catch (error) { + logger.error('Disconnected from the node1', { error }); } reconnectionsCounter++; @@ -64,7 +64,7 @@ async function reconnect(api: GearApi, indexer: GearIndexer, cb: GenesisCb) { reconnectionsCounter = 0; } - logger.info('⚙️ 📡 Reconnecting to the gear node...'); + logger.info('Attempting to reconnect'); return connectToNode(indexer, cb); } diff --git a/idea/indexer/src/gear/indexer.ts b/idea/indexer/src/gear/indexer.ts index bba517ad28..d69a928ca1 100644 --- a/idea/indexer/src/gear/indexer.ts +++ b/idea/indexer/src/gear/indexer.ts @@ -12,7 +12,7 @@ import { filterEvents } from '@polkadot/api/util'; import { GenericEventData } from '@polkadot/types'; import { ExtrinsicStatus } from '@polkadot/types/interfaces'; import { SignedBlockExtended } from '@polkadot/api-derive/types'; -import { EventNames, CodeStatus } from '@gear-js/common'; +import { EventNames, CodeStatus, logger } from '@gear-js/common'; import { VoidFn } from '@polkadot/api/types'; import { Option } from '@polkadot/types'; @@ -23,7 +23,6 @@ import { eventDataHandlers, MessageEntryPoint, MessageType, - logger, UserMessageSentInput, UserMessageReadInput, ProgramChangedInput, @@ -113,7 +112,7 @@ export class GearIndexer { } } if (this.api === null) { - console.log('api null'); + logger.warn('api null'); continue; } @@ -129,9 +128,8 @@ export class GearIndexer { try { block = await this.api.derive.chain.getBlockByNumber(blockNumber); - } catch (err) { - logger.error(`Unable to get block ${blockNumber} due to the following error`); - console.log(err); + } catch (error) { + logger.error(`Unable to get block ${blockNumber}`, { error }); return; } @@ -152,8 +150,8 @@ export class GearIndexer { try { await this.tempState.save(); - } catch (err) { - logger.error(`Error during saving the data of the block ${blockNumber}. ${err.message}`); + } catch (error) { + logger.error(`Error during saving the data of the block ${blockNumber}`, { error }); } if (this.oneTimeSync) { await this.statusService.update(this.genesis, blockNumber.toString(), hash); @@ -185,36 +183,34 @@ export class GearIndexer { private async handleEvents(block: SignedBlockExtended, timestamp: number): Promise { const necessaryEvents = block.events.filter(({ event: { method } }) => Object.keys(EventNames).includes(method)); + const blockHash = block.block.header.hash.toHex(); for (const { event: { data, method }, } of necessaryEvents) { let eventData = null; try { eventData = eventDataHandlers[method](data as GenericEventData); - } catch (err) { - logger.warn(`Unable to form event data, ${JSON.stringify({ method, data: data.toHuman() })}`); - console.log(err); + } catch (error) { + logger.warn('Unable to form event data.', { + error, + method, + data: data.toJSON(), + blockHash, + }); continue; } if (eventData === null) continue; - const blockHash = block.block.header.hash.toHex(); try { await this.eventHandlers[method](eventData, timestamp, blockHash); } catch (error) { - logger.warn( - JSON.stringify( - { - method, - data: eventData, - blockHash, - }, - undefined, - 2, - ), - ); - console.error(error); + logger.warn('Unable to handle an event.', { + error, + method, + data: eventData, + blockHash, + }); } } } diff --git a/idea/indexer/src/healthcheck.server.ts b/idea/indexer/src/healthcheck.server.ts index a57170211e..2b94027c47 100644 --- a/idea/indexer/src/healthcheck.server.ts +++ b/idea/indexer/src/healthcheck.server.ts @@ -1,7 +1,7 @@ import http from 'http'; +import { logger } from '@gear-js/common'; import config from './config'; -import { logger } from './common'; export const statuses = { rmq: false, diff --git a/idea/indexer/src/main.ts b/idea/indexer/src/main.ts index b3f40363b5..bebb10ba8e 100644 --- a/idea/indexer/src/main.ts +++ b/idea/indexer/src/main.ts @@ -1,5 +1,5 @@ import { waitReady } from '@polkadot/wasm-crypto'; -import { RMQServiceActions } from '@gear-js/common'; +import { RMQServiceActions, logger } from '@gear-js/common'; import { changeStatus, runHealthcheckServer } from './healthcheck.server'; import { AppDataSource } from './database'; @@ -17,6 +17,8 @@ async function bootstrap() { const dataSource = await AppDataSource.initialize(); + logger.info('DB connection established'); + changeStatus('database'); await waitReady(); diff --git a/idea/indexer/src/one-time-sync.ts b/idea/indexer/src/one-time-sync.ts index c5fd05d823..8f3927a277 100644 --- a/idea/indexer/src/one-time-sync.ts +++ b/idea/indexer/src/one-time-sync.ts @@ -1,3 +1,4 @@ +import { logger } from '@gear-js/common'; import { waitReady } from '@polkadot/wasm-crypto'; import { GearApi } from '@gear-js/api'; @@ -9,7 +10,6 @@ import { MessageService } from './services'; import { ProgramService } from './services'; import { GearIndexer } from './gear'; import config from './config'; -import { logger } from './common'; import { RMQService } from './rmq'; async function bootstrap() { @@ -38,7 +38,7 @@ async function bootstrap() { } await api.isReady; const genesis = api.genesisHash.toHex(); - logger.info(`⚙️ Connected to ${api.runtimeChain} with genesis ${genesis}`); + logger.info(`Connected to ${api.runtimeChain} with genesis ${genesis}`); let fromBlock = 1; const status = await statusService.getStatus(genesis); @@ -81,7 +81,7 @@ async function bootstrap() { bootstrap() .then(() => process.exit(0)) - .catch((err) => { - console.log(err); + .catch((error) => { + logger.error('', { error }); process.exit(1); }); diff --git a/idea/indexer/src/rmq.ts b/idea/indexer/src/rmq.ts index 84b9c0f831..889e4dd0b3 100644 --- a/idea/indexer/src/rmq.ts +++ b/idea/indexer/src/rmq.ts @@ -8,9 +8,9 @@ import { RMQExchanges, RMQQueues, INDEXER_INTERNAL_METHODS, + logger, } from '@gear-js/common'; -import { logger } from './common'; import { BlockService, CodeService, MessageService, ProgramService, StateService } from './services'; import config from './config'; @@ -49,7 +49,7 @@ export class RMQService { this.connection = await connect(config.rabbitmq.url); this.connection.on('close', (error) => { - console.log(new Date(), error); + logger.error('RabbitMQ connection lost', { error }); process.exit(1); }); @@ -78,7 +78,7 @@ export class RMQService { `${RMQServices.INDEXER}.meta`, ); } catch (error) { - console.log(error); + logger.error('Unable to setup rabbitmq exchanges', { error }); throw error; } } @@ -131,13 +131,13 @@ export class RMQService { const { method } = msg.properties.headers; const params = JSON.parse(msg.content.toString()); - console.log(method, params); + logger.info('Message from meta-storage', { method, params }); await this.handleIncomingMsg(method, params); }, { noAck: true }, ); } catch (error) { - logger.error(JSON.stringify(error, undefined, 2)); + logger.error('Meta message consumer error', { error }); } } @@ -160,7 +160,7 @@ export class RMQService { { noAck: true }, ); } catch (error) { - logger.error(`Direct exchange consumer ${JSON.stringify(error)}`); + logger.error('Direct exchange consumer error.', { error }); } } @@ -179,7 +179,7 @@ export class RMQService { { noAck: true }, ); } catch (error) { - logger.error(`Topic exchange consumer ${JSON.stringify(error)}`); + logger.error('Topic exchange consumer error.', { error }); } } diff --git a/idea/indexer/src/services/message.service.ts b/idea/indexer/src/services/message.service.ts index 37c42b50b4..02793dc4f0 100644 --- a/idea/indexer/src/services/message.service.ts +++ b/idea/indexer/src/services/message.service.ts @@ -4,6 +4,7 @@ import { FindMessageParams, GetMessagesParams, MessageReadReason, + logger, ProgramStatus, } from '@gear-js/common'; @@ -15,7 +16,6 @@ import { MessageNotFound, constructQueryBuilder, PAGINATION_LIMIT, - logger, } from '../common'; export class MessageService { @@ -79,7 +79,7 @@ export class MessageService { try { await this.repo.update({ id, genesis }, { processedWithPanic: statuses[id] === 'Success' ? false : true }); } catch (error) { - logger.error(error); + logger.error(error.message, { error }); } if (statuses[id] === 'Failed') { @@ -95,7 +95,7 @@ export class MessageService { try { await this.repo.update({ id }, { readReason }); } catch (error) { - logger.error(error); + logger.error(error.message, { error }); } } diff --git a/idea/indexer/src/services/program.service.ts b/idea/indexer/src/services/program.service.ts index 3c94418a53..3d5f4a5af6 100644 --- a/idea/indexer/src/services/program.service.ts +++ b/idea/indexer/src/services/program.service.ts @@ -6,6 +6,7 @@ import { GetAllProgramsResult, IProgram, ProgramStatus, + logger, } from '@gear-js/common'; import { ProgramNotFound } from '../common/errors'; @@ -78,10 +79,9 @@ export class ProgramService { try { const programs = await this.repo.save(program); - return programs[0]; } catch (error) { - console.log(error); + logger.error('Unable to set program status', { error }); } } diff --git a/idea/meta-storage/src/logger.ts b/idea/meta-storage/src/logger.ts deleted file mode 100644 index b47cfaa1c6..0000000000 --- a/idea/meta-storage/src/logger.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { getLogger } from '@gear-js/common'; - -export const logger = getLogger(); diff --git a/idea/meta-storage/src/main.ts b/idea/meta-storage/src/main.ts index 201254b269..dc9aa9826a 100644 --- a/idea/meta-storage/src/main.ts +++ b/idea/meta-storage/src/main.ts @@ -1,5 +1,6 @@ +import { logger } from '@gear-js/common'; + import { AppDataSource } from './database'; -import { logger } from './logger'; import { RMQService } from './rmq'; import { MetaService } from './service'; diff --git a/idea/meta-storage/src/rmq.ts b/idea/meta-storage/src/rmq.ts index d7da39ff3a..b40a2070ae 100644 --- a/idea/meta-storage/src/rmq.ts +++ b/idea/meta-storage/src/rmq.ts @@ -7,9 +7,9 @@ import { RMQExchanges, RMQQueues, INDEXER_INTERNAL_METHODS, + logger, } from '@gear-js/common'; -import { logger } from './logger'; import config from './config'; import { MetaService } from './service'; @@ -48,11 +48,11 @@ export class RMQService { await this.directMsgConsumer(RMQServices.META_STORAGE); this.connection.on('close', (error) => { - console.log(new Date(), error); + logger.error('RabbitMQ connection closed', { error }); process.exit(1); }); } catch (error) { - console.log(error); + logger.error('Unable to setup rabbitmq exchanges', { error, stack: error.stack }); throw error; } } @@ -93,7 +93,7 @@ export class RMQService { { noAck: true }, ); } catch (error) { - logger.error(`Direct exchange consumer ${JSON.stringify(error)}`); + logger.error('Direct exchange consumer error.', { error, stack: error.stack }); } } diff --git a/idea/test-balance/src/common/logger.ts b/idea/test-balance/src/common/logger.ts deleted file mode 100644 index 33c120603b..0000000000 --- a/idea/test-balance/src/common/logger.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getLogger } from '@gear-js/common'; - -export const TEST_BALANCE = process.env.NODE_ENV === 'dev' ? 'TEST_BALANCE' : ''; - -export const logger = getLogger(TEST_BALANCE); diff --git a/idea/test-balance/src/common/transfer-balance-process.ts b/idea/test-balance/src/common/transfer-balance-process.ts index 33030b4cda..b34285af60 100644 --- a/idea/test-balance/src/common/transfer-balance-process.ts +++ b/idea/test-balance/src/common/transfer-balance-process.ts @@ -1,13 +1,17 @@ -import { JSONRPC_ERRORS, RMQExchanges, RMQQueues } from '@gear-js/common'; +import { logger, JSONRPC_ERRORS, RMQExchanges, RMQQueues } from '@gear-js/common'; import { EventEmitter } from 'node:events'; -import { transferService } from '../services/transfer.service'; +import { transferService } from '../transfer.service'; import { gearService } from '../gear'; -import { logger } from './logger'; import { producer } from '../rabbitmq/producer'; -import { TBRequestParams } from './types'; + +interface TBRequestParams { + payload: { address: string; genesis: string }; + correlationId: string; +} export const requests: Array = []; + const pushEmitter = new EventEmitter(); Object.defineProperty(requests, 'push', { @@ -47,7 +51,7 @@ export async function transferProcess(): Promise { result = { error: JSONRPC_ERRORS.TransferLimitReached.name }; } } catch (error) { - logger.error(error.message, error.stack); + logger.error(error.message, { stack: error.stack }); result = { error: JSONRPC_ERRORS.InternalError.name }; } producer.sendMessage(RMQExchanges.DIRECT_EX, RMQQueues.REPLIES, correlationId, result); diff --git a/idea/test-balance/src/common/types.ts b/idea/test-balance/src/common/types.ts deleted file mode 100644 index b3a535ac09..0000000000 --- a/idea/test-balance/src/common/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface TBRequestParams { - payload: { address: string; genesis: string }; - correlationId: string; -} diff --git a/idea/test-balance/src/gear/connection.ts b/idea/test-balance/src/gear/connection.ts index 47d0315ce0..e058da8687 100644 --- a/idea/test-balance/src/gear/connection.ts +++ b/idea/test-balance/src/gear/connection.ts @@ -1,9 +1,9 @@ import { GearApi, HexString } from '@gear-js/api'; +import { logger } from '@gear-js/common'; -import { changeStatus } from '../routes/healthcheck.router'; +import { changeStatus } from '../healthcheck.router'; import config from '../config/configuration'; import { producer } from '../rabbitmq/producer'; -import { logger } from '../common/logger'; export let api: GearApi; let genesisHash: HexString; @@ -25,7 +25,7 @@ export async function connect() { try { await api.isReadyOrError; } catch (error) { - logger.error(`Failed to connect to ${providerAddress}`); + logger.error(`Failed to connect to ${providerAddress}`, { error }); await reconnect(); } await api.isReady; @@ -50,7 +50,7 @@ async function reconnect(): Promise { reconnectionsCounter = 0; } - logger.info('⚙️ 📡 Reconnecting to the gear node...'); + logger.info('Attempting to reconnect'); changeStatus('ws'); return connect(); } diff --git a/idea/test-balance/src/gear/index.ts b/idea/test-balance/src/gear/index.ts index e6683cdebc..44dc69af7b 100644 --- a/idea/test-balance/src/gear/index.ts +++ b/idea/test-balance/src/gear/index.ts @@ -1,13 +1,18 @@ import { TransferData } from '@gear-js/api'; import { KeyringPair } from '@polkadot/keyring/types'; import { BN } from '@polkadot/util'; +import { logger } from '@gear-js/common'; import config from '../config/configuration'; -import { ResponseTransferBalance } from './types'; -import { transferService } from '../services/transfer.service'; +import { transferService } from '../transfer.service'; import { createAccount } from './utils'; import { connect, api, getGenesisHash } from './connection'; -import { logger } from '../common/logger'; + +interface ResponseTransferBalance { + status?: string; + transferredBalance?: string; + error?: string; +} let tbAccount: KeyringPair; let prefundedAcc: KeyringPair; @@ -32,11 +37,11 @@ async function transferBalance( from: KeyringPair = tbAccount, balance: BN = balanceToTransfer, ): Promise { - logger.info(`Transfer value ${balance.toNumber()} from ${from.address} to ${to}`); + logger.info(`Transfer value`, { from: from.address, to, amount: balance.toString() }); try { await transfer(to, from, balance); } catch (error) { - logger.error(error); + logger.error('Transfer balance error', { error }); return { error: `Transfer balance from ${from} to ${to} failed` }; } if (to !== tbAccount.address) { diff --git a/idea/test-balance/src/gear/types.ts b/idea/test-balance/src/gear/types.ts deleted file mode 100644 index bf37e7f8f6..0000000000 --- a/idea/test-balance/src/gear/types.ts +++ /dev/null @@ -1,7 +0,0 @@ -interface ResponseTransferBalance { - status?: string; - transferredBalance?: string; - error?: string; -} - -export { ResponseTransferBalance }; diff --git a/idea/test-balance/src/routes/healthcheck.router.ts b/idea/test-balance/src/healthcheck.router.ts similarity index 100% rename from idea/test-balance/src/routes/healthcheck.router.ts rename to idea/test-balance/src/healthcheck.router.ts diff --git a/idea/test-balance/src/main.ts b/idea/test-balance/src/main.ts index 9d3e73ff01..d0bbb44df6 100644 --- a/idea/test-balance/src/main.ts +++ b/idea/test-balance/src/main.ts @@ -2,11 +2,12 @@ import express from 'express'; import config from './config/configuration'; -import { changeStatus, healthcheckRouter } from './routes/healthcheck.router'; +import { changeStatus, healthcheckRouter } from './healthcheck.router'; import { connectToDB } from './database/app-data-source'; import { gearService } from './gear'; -import { initAMQ } from './rabbitmq/init-rabbitmq'; +import { initAMQ } from './rabbitmq/rmq'; import { transferProcess } from './common/transfer-balance-process'; +import { logger } from '@gear-js/common'; const app = express(); @@ -16,7 +17,7 @@ app.use('/health', healthcheckRouter); const startApp = async () => { app.listen(port, () => { - console.log(`Healthckech server is running on port ${port} 🚀`); + logger.info(`Healthckech server is running on port ${port}`); }); await connectToDB(); diff --git a/idea/test-balance/src/rabbitmq/consumer.ts b/idea/test-balance/src/rabbitmq/consumer.ts index cebbddcfab..41570fa249 100644 --- a/idea/test-balance/src/rabbitmq/consumer.ts +++ b/idea/test-balance/src/rabbitmq/consumer.ts @@ -1,5 +1,5 @@ import { Channel, Replies } from 'amqplib'; -import { TEST_BALANCE_METHODS } from '@gear-js/common'; +import { TEST_BALANCE_METHODS, logger } from '@gear-js/common'; import { producer } from './producer'; import { gearService } from '../gear'; @@ -21,7 +21,7 @@ export async function directMessageConsumer(channel: Channel, queue: string): Pr { noAck: true }, ); } catch (error) { - console.error(`${new Date()} | Direct exchange consumer error`, error); + logger.error(`Direct exchange consumer error`, { error }); } } @@ -39,7 +39,6 @@ export async function topicMessageConsumer(channel: Channel, repliesAssertQueue: { noAck: true }, ); } catch (error) { - this.logger.error(new Date()); - this.logger.error(`${new Date()} | Topic exchange consumer error ${JSON.stringify(error)}`); + logger.error(`Topic exchange consumer error`, { error }); } } diff --git a/idea/test-balance/src/rabbitmq/producer.ts b/idea/test-balance/src/rabbitmq/producer.ts index 253bcea5ae..cb6d78c689 100644 --- a/idea/test-balance/src/rabbitmq/producer.ts +++ b/idea/test-balance/src/rabbitmq/producer.ts @@ -1,6 +1,6 @@ import { RMQExchanges, RMQQueues, RMQServiceActions, RMQServices } from '@gear-js/common'; -import { mainChannelAMQP } from './init-rabbitmq'; +import { mainChannelAMQP } from './rmq'; function sendGenesis(genesis: string): void { const messageBuff = JSON.stringify({ service: RMQServices.TEST_BALANCE, action: RMQServiceActions.ADD, genesis }); diff --git a/idea/test-balance/src/rabbitmq/init-rabbitmq.ts b/idea/test-balance/src/rabbitmq/rmq.ts similarity index 84% rename from idea/test-balance/src/rabbitmq/init-rabbitmq.ts rename to idea/test-balance/src/rabbitmq/rmq.ts index 3cc436f0f8..766544f158 100644 --- a/idea/test-balance/src/rabbitmq/init-rabbitmq.ts +++ b/idea/test-balance/src/rabbitmq/rmq.ts @@ -1,5 +1,5 @@ import { Channel, connect, Connection } from 'amqplib'; -import { RMQExchanges, RMQQueues, RMQServiceActions, RMQServices } from '@gear-js/common'; +import { logger, RMQExchanges, RMQQueues, RMQServiceActions, RMQServices } from '@gear-js/common'; import config from '../config/configuration'; import { gearService } from '../gear'; @@ -10,7 +10,12 @@ export let mainChannelAMQP: Channel; export let topicChannelAMQP: Channel; export async function initAMQ(): Promise { - connectionAMQP = await connectAMQP(config.rabbitmq.url); + try { + connectionAMQP = await connect(config.rabbitmq.url); + } catch (error) { + logger.error('RabbitMQ connection error', { error }); + } + mainChannelAMQP = await connectionAMQP.createChannel(); topicChannelAMQP = await connectionAMQP.createChannel(); const directExchange = RMQExchanges.DIRECT_EX; @@ -44,15 +49,7 @@ export async function initAMQ(): Promise { await topicMessageConsumer(topicChannelAMQP, assertTopicQueue); connectionAMQP.on('close', (error) => { - console.log(new Date(), error); + logger.error('RabbitMQ connection closed', { error }); process.exit(1); }); } - -async function connectAMQP(url: string): Promise { - try { - return connect(url); - } catch (error) { - console.error(`${new Date()} | RabbitMQ connection error`, error); - } -} diff --git a/idea/test-balance/src/rabbitmq/types/index.ts b/idea/test-balance/src/rabbitmq/types/index.ts deleted file mode 100644 index 7bf7b5a4e4..0000000000 --- a/idea/test-balance/src/rabbitmq/types/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -interface IRMQMessage { - service: null | string; - action: null | string; - genesis: string; - params: any; -} - -export { IRMQMessage }; diff --git a/idea/test-balance/src/services/transfer.service.ts b/idea/test-balance/src/transfer.service.ts similarity index 86% rename from idea/test-balance/src/services/transfer.service.ts rename to idea/test-balance/src/transfer.service.ts index 7e0bdc6fc4..655fdf78e7 100644 --- a/idea/test-balance/src/services/transfer.service.ts +++ b/idea/test-balance/src/transfer.service.ts @@ -1,7 +1,7 @@ import { plainToClass } from 'class-transformer'; -import { TransferBalance } from '../database/entities/transfer.entity'; -import { transferRepository } from '../database/repositories/transfer.repository'; +import { TransferBalance } from './database/entities/transfer.entity'; +import { transferRepository } from './database/repositories/transfer.repository'; const transferService = { async setTransferDate(account: string, genesis: string): Promise {