Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging rework #2740

Draft
wants to merge 1 commit into
base: v6/develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ process.env.NODE_ENV =
})();

process.on('uncaughtException', (err) => {
console.error('Something went really wrong! OT-node shutting down...', err);
console.error(`Something went really wrong! Error: ${err.message}. OT-node shutting down...`);
process.exit(1);
});
98 changes: 55 additions & 43 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,23 @@ class OTNode {
this.logger.warn(`Using node.js version: ${process.versions.node}`);
if (nodeMajorVersion < MIN_NODE_VERSION) {
this.logger.warn(
`This node was tested with node.js version 16. To make sure that your node is running properly please update your node version!`,
`This node was tested with node.js version 16. ` +
`To make sure that your node is running properly please update your node version!`,
);
}
this.logger.warn('======================================================');
}

initializeLogger() {
this.logger = new Logger(this.config.logLevel);

this.logger.info('Logger has been successfully initialized.');
}

initializeFileService() {
this.fileService = new FileService({ config: this.config, logger: this.logger });

this.logger.info('File Service has been successfully initialized.');
}

initializeAutoUpdaterModule() {
Expand All @@ -100,6 +105,8 @@ class OTNode {
this.config.modules.autoUpdater.implementation['ot-auto-updater'].config,
this.logger,
);

this.logger.info('Auto Updater has been successfully initialized.');
}

initializeConfiguration(userConfig) {
Expand All @@ -121,7 +128,7 @@ class OTNode {
DependencyInjection.registerValue(this.container, 'config', this.config);
DependencyInjection.registerValue(this.container, 'logger', this.logger);

this.logger.info('Dependency injection module is initialized');
this.logger.info('Dependency Injection Module has been successfully initialized.');
}

async initializeModules() {
Expand All @@ -134,59 +141,58 @@ class OTNode {
}
try {
await Promise.all(initializationPromises);
this.logger.info(`All modules initialized!`);
} catch (e) {
this.logger.error(`Module initialization failed. Error message: ${e.message}`);
this.logger.error(
`Module initialization failed. Error: ${e.message}. ` +
`OT-Node is shutting down...`,
);
this.stop(1);
}

this.logger.info(`All modules have been successfully initialized.`);
}

initializeEventEmitter() {
const eventEmitter = new EventEmitter();
DependencyInjection.registerValue(this.container, 'eventEmitter', eventEmitter);

this.logger.info('Event emitter initialized');
this.logger.info('Event Emitter has been successfully initialized.');
}

async initializeBlockchainEventListenerService() {
try {
const eventListenerService = this.container.resolve('blockchainEventListenerService');
await eventListenerService.initialize();
eventListenerService.startListeningOnEvents();
this.logger.info('Event Listener Service initialized successfully');
} catch (error) {
this.logger.error(
`Unable to initialize event listener service. Error message: ${error.message} OT-node shutting down...`,
`Unable to initialize Event Listener Service. Error: ${error.message}. ` +
`OT-Node is shutting down...`,
);
this.stop(1);
}

this.logger.info('Event Listener Service has been successfully initialized.');
}

async initializeRouters() {
try {
this.logger.info('Initializing http api and rpc router');

const routerNames = ['httpApiRouter', 'rpcRouter'];
await Promise.all(
routerNames.map(async (routerName) => {
const router = this.container.resolve(routerName);
try {
await router.initialize();
} catch (error) {
this.logger.error(
`${routerName} initialization failed. Error message: ${error.message}, ${error.stackTrace}`,
);
this.stop(1);
}
}),
);
this.logger.info('Routers initialized successfully');
} catch (error) {
this.logger.error(
`Failed to initialize routers: ${error.message}, ${error.stackTrace}`,
);
this.stop(1);
}
const routerNames = ['httpApiRouter', 'rpcRouter'];
await Promise.all(
routerNames.map(async (routerName) => {
const router = this.container.resolve(routerName);
try {
await router.initialize();
} catch (error) {
this.logger.error(
`${routerName} initialization failed. Error: ${error.message}. ` +
`OT-Node is shutting down...`,
);
this.stop(1);
}
}),
);

this.logger.info('HTTP API and RPC Routers are successfully initialized.');
}

async createProfiles() {
Expand All @@ -196,7 +202,7 @@ class OTNode {
.map(async (blockchain) => {
try {
if (!(await blockchainModuleManager.identityIdExists(blockchain))) {
this.logger.info(`Creating profile on network: ${blockchain}`);
this.logger.info(`Creating profile on ${blockchain} network.`);
const networkModuleManager = this.container.resolve('networkModuleManager');
const peerId = networkModuleManager.getPeerId().toB58String();
await blockchainModuleManager.createProfile(blockchain, peerId);
Expand Down Expand Up @@ -225,10 +231,10 @@ class OTNode {
}
}
const identityId = await blockchainModuleManager.getIdentityId(blockchain);
this.logger.info(`Identity ID: ${identityId}`);
this.logger.info(`Identity ID: ${identityId}.`);
} catch (error) {
this.logger.warn(
`Unable to create ${blockchain} blockchain profile. Removing implementation. Error: ${error.message}`,
`Unable to create ${blockchain} blockchain profile. Error: ${error.message}. Removing implementation...`,
);
blockchainModuleManager.removeImplementation(blockchain);
}
Expand All @@ -237,7 +243,7 @@ class OTNode {
await Promise.all(createProfilesPromises);

if (!blockchainModuleManager.getImplementationNames().length) {
this.logger.error(`Unable to create blockchain profiles. OT-node shutting down...`);
this.logger.error(`Unable to create blockchain profiles. OT-Node is shutting down...`);
this.stop(1);
}
}
Expand All @@ -249,10 +255,11 @@ class OTNode {
await commandExecutor.addDefaultCommands();
commandExecutor
.replayOldCommands()
.then(() => this.logger.info('Finished replaying old commands'));
.then(() => this.logger.info('Finished replaying old commands.'));
} catch (e) {
this.logger.error(
`Command executor initialization failed. Error message: ${e.message}`,
`Command executor initialization failed. Error: ${e.message}. ` +
`OT-Node is shutting down...`,
);
this.stop(1);
}
Expand All @@ -273,6 +280,8 @@ class OTNode {
async startNetworkModule() {
const networkModuleManager = this.container.resolve('networkModuleManager');
await networkModuleManager.start();

this.logger.info('Network Module has been successfully started.');
}

async executePrivateAssetsMetadataMigration() {
Expand Down Expand Up @@ -465,13 +474,15 @@ class OTNode {
try {
const shardingTableService = this.container.resolve('shardingTableService');
await shardingTableService.initialize();
this.logger.info('Sharding Table Service initialized successfully');
} catch (error) {
this.logger.error(
`Unable to initialize sharding table service. Error message: ${error.message} OT-node shutting down...`,
`Unable to initialize sharding table service. Error: ${error.message} ` +
`OT-Node is shutting down...`,
);
this.stop(1);
}

this.logger.info('Sharding Table Service has been successfully initialized.');
}

async initializeTelemetryInjectionService() {
Expand All @@ -481,19 +492,20 @@ class OTNode {
'telemetryInjectionService',
);
telemetryHubModuleManager.initialize();
this.logger.info('Telemetry Injection Service initialized successfully');
} catch (e) {
this.logger.error(
`Telemetry hub module initialization failed. Error message: ${e.message}`,
`Telemetry hub module initialization failed. Error: ${e.message}`,
);
}

this.logger.info('Telemetry Injection Service has been successfully initialized.');
}
}

async removeUpdateFile() {
const updateFilePath = this.fileService.getUpdateFilePath();
await this.fileService.removeFile(updateFilePath).catch((error) => {
this.logger.warn(`Unable to remove update file. Error: ${error}`);
this.logger.warn(`Unable to remove update file. Error: ${error}.`);
});
this.config.otNodeUpdated = true;
}
Expand All @@ -510,7 +522,7 @@ class OTNode {
}

stop(code = 0) {
this.logger.info('Stopping node...');
this.logger.info('Stopping the node...');
process.exit(code);
}

Expand Down
18 changes: 9 additions & 9 deletions scripts/copy-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ function logPercentage(index, max) {

if (Math.floor(currentPercentage) - Math.floor(previousPercentage) < 1) return;

logger.debug(`Migration at ${Math.floor(currentPercentage * 10) / 10}%`);
logger.debug(`Migration at ${Math.floor(currentPercentage * 10) / 10}%.`);
}

let toRepositoryAssertions = await getAssertions(toImplementation, toRepository);
logger.info(
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository before migration`,
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository before migration.`,
);

logger.info(
`Starting to copy assertions from ${fromImplementation} repository ${fromRepository} with name ${fromRepositoryName} to repository ${toImplementation} repository ${toRepository} with name ${toRepositoryName}`,
`Starting to copy assertions from ${fromImplementation} repository ${fromRepository} ``with name ${fromRepositoryName} to repository ${toImplementation} repository ${toRepository} ``with name ${toRepositoryName}.`,
);

const fromRepositoryAssertions = await getAssertions(fromImplementation, fromRepository);
logger.info(`${fromRepositoryAssertions.length} assertions found in ${fromRepository}`);
logger.info(`${fromRepositoryAssertions.length} assertions found in ${fromRepository}.`);

let completed = 0;
const copyAssertion = async (g) => {
Expand All @@ -101,9 +101,9 @@ const copyAssertion = async (g) => {
logger.error(
`Error while getting assertion ${g.substring(
'assertion:'.length,
)} from ${fromImplementation} repository ${fromRepository} with name ${fromRepositoryName}. Error: ${
)} from ${fromImplementation} repository ${fromRepository} with name ${fromRepositoryName}; Error: ${
error.message
}`,
}.`,
);
process.exit(1);
}
Expand All @@ -119,9 +119,9 @@ const copyAssertion = async (g) => {
logger.error(
`Error while inserting assertion ${g.substring(
'assertion:'.length,
)} with nquads: ${nquads} in ${toImplementation} repository ${toRepository} with name ${toRepositoryName}. Error: ${
)} with nquads: ${nquads} in ${toImplementation} repository ${toRepository} with name ${toRepositoryName}; Error: ${
error.message
}`,
}.`,
);
process.exit(1);
}
Expand Down Expand Up @@ -150,5 +150,5 @@ logger.info(`Migration completed! Lasted ${(end - start) / 1000} seconds.`);

toRepositoryAssertions = await getAssertions(toImplementation, toRepository);
logger.info(
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository after migration`,
`${toRepositoryAssertions.length} assertions found in ${toRepository} repository after migration.`,
);
14 changes: 9 additions & 5 deletions src/commands/cleaners/cleaner-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import Command from '../command.js';
class CleanerCommand extends Command {
constructor(ctx) {
super(ctx);
this.logger = ctx.logger;
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.archiveService = ctx.archiveService;
}

/**
* Executes command and produces one or more events
* @param command
*/
async execute() {
const nowTimestamp = Date.now();
Expand Down Expand Up @@ -49,16 +49,16 @@ class CleanerCommand extends Command {

// eslint-disable-next-line no-unused-vars
async findRowsForRemoval(nowTimestamp) {
throw Error('findRowsForRemoval not implemented');
throw Error('findRowsForRemoval is not implemented.');
}

getArchiveFolderName() {
throw Error('getArchiveFolderName not implemented');
throw Error('getArchiveFolderName is not implemented.');
}

// eslint-disable-next-line no-unused-vars
async deleteRows(ids) {
throw Error('deleteRows not implemented');
throw Error('deleteRows is not implemented.');
}

/**
Expand All @@ -67,7 +67,11 @@ class CleanerCommand extends Command {
* @param error
*/
async recover(command, error) {
this.logger.warn(`Failed to clean operational db data: error: ${error.message}`);
this.logger.warn(
`Error occurred during the command execution; ` +
`Error Message: ${error.message}. Repeating the command...`,
command,
);
return Command.repeat();
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/commands/cleaners/operation-id-cleaner-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ class OperationIdCleanerCommand extends Command {

/**
* Executes command and produces one or more events
* @param command
*/
async execute() {
this.logger.debug('Starting command for removal of expired cache files');
async execute(command) {
const timeToBeDeleted = Date.now() - OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS;
await this.repositoryModuleManager.removeOperationIdRecord(timeToBeDeleted, [
OPERATION_ID_STATUS.COMPLETED,
Expand All @@ -33,17 +31,20 @@ class OperationIdCleanerCommand extends Command {
);
if (removed) {
this.logger.debug(
`Successfully removed ${
removed / BYTES_IN_KILOBYTE
} Kbs expired cached operation entries from memory`,
`Successfully removed ${removed / BYTES_IN_KILOBYTE} KBs ` +
`of expired cached operation entries from the memory.`,
command,
);
}
removed = await this.operationIdService.removeExpiredOperationIdFileCache(
OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS,
OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER,
);
if (removed) {
this.logger.debug(`Successfully removed ${removed} expired cached operation files`);
this.logger.debug(
`Successfully removed ${removed} expired cached operation files.`,
command,
);
}

return Command.repeat();
Expand All @@ -55,7 +56,11 @@ class OperationIdCleanerCommand extends Command {
* @param error
*/
async recover(command, error) {
this.logger.warn(`Failed to clean operation ids table: error: ${error.message}`);
this.logger.warn(
`Error occurred during the command execution; ` +
`Error Message: ${error.message}. Repeating the command...`,
command,
);
return Command.repeat();
}

Expand Down
Loading