Skip to content

Commit

Permalink
feat: custom logger for lib (#480)
Browse files Browse the repository at this point in the history
* feat: custom logger for lib

* chore: bump lib v1.12.1
  • Loading branch information
r4mmer authored Sep 6, 2024
1 parent c58cb44 commit 31d66b1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
19 changes: 15 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@dinamonetworks/hsm-dinamo": "4.9.1",
"@hathor/healthcheck-lib": "0.1.0",
"@hathor/wallet-lib": "1.10.0",
"@hathor/wallet-lib": "1.12.1",
"axios": "1.7.2",
"express": "4.18.2",
"express-validator": "6.10.0",
Expand Down
65 changes: 63 additions & 2 deletions src/services/logger.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import { HathorWallet } from '@hathor/wallet-lib';
import { buildAppLogger, buildServiceLogger } from '../logger';

/**
Expand All @@ -13,13 +14,36 @@ import { buildAppLogger, buildServiceLogger } from '../logger';
*/
const walletLoggers = new Map();

/**
* @typedef {Object} ILogger
* @property {(...args) => void} debug - Log message at the debug level
* @property {(...args) => void} info - Log message at the info level
* @property {(...args) => void} warn - Log message at the warn level
* @property {(...args) => void} error - Log message at the error level
*/

/**
* @param {import('winston').Logger} logger
* @returns {ILogger}
*/
function buildLibLogger(logger) {
return {
debug: (...args) => logger.debug.call(logger, ...args),
info: (...args) => logger.info.call(logger, ...args),
warn: (...args) => logger.warn.call(logger, ...args),
error: (...args) => logger.error.call(logger, ...args),

Check warning on line 34 in src/services/logger.service.js

View check run for this annotation

Codecov / codecov/patch

src/services/logger.service.js#L33-L34

Added lines #L33 - L34 were not covered by tests
};
}

/**
* Initialize a wallet logger
* @param {string} walletId
* @returns {[import('winston').Logger, ILogger]}
*/
function initializeWalletLogger(walletId) {
const logger = buildServiceLogger(`wallet(${walletId})`);
walletLoggers.set(walletId, logger);
const libLogger = buildLibLogger(logger);
return [logger, libLogger];
}

/**
Expand Down Expand Up @@ -48,4 +72,41 @@ function getLogger(req) {
return logger;
}

export { walletLoggers, initializeWalletLogger, getLogger };
/**
* @param {HathorWallet} wallet
* @param {import('winston').Logger} logger
*/
function setupWalletStateLogs(wallet, logger) {
const times = {
connecting: 0,
syncing: 0,
processing: 0,
ready: 0,
};
wallet.on('state', state => {
switch (state) {
case HathorWallet.CONNECTING:
times.connecting = Date.now();
break;
case HathorWallet.SYNCING:
times.syncing = Date.now();
logger.debug(`state_update[syncing]: time to connect ${(times.syncing - times.connecting) / 1000} seconds`);
break;
case HathorWallet.PROCESSING:
times.processing = Date.now();
logger.debug(`state_update[processing]: time to sync ${(times.processing - times.syncing) / 1000} seconds`);
break;
case HathorWallet.READY:
times.ready = Date.now();
logger.debug(`state_update[ready]: time to process history ${(times.ready - times.processing) / 1000} seconds`);
break;
default:
break;
}
});
wallet.conn.on('wallet-load-partial-update', data => {
logger.debug(`Found ${data.addressesFound} addresses and ${data.historyLength} transactions`);
});
}

export { walletLoggers, initializeWalletLogger, getLogger, setupWalletStateLogs };
10 changes: 8 additions & 2 deletions src/services/wallets.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { removeAllWalletProposals } = require('./atomic-swap.service');
const { notificationBus } = require('./notification.service');
const { sanitizeLogInput } = require('../logger');
const { lock } = require('../lock');
const { walletLoggers, initializeWalletLogger } = require('./logger.service');
const { walletLoggers, initializeWalletLogger, setupWalletStateLogs } = require('./logger.service');

/**
* All wallets that were initialized by the user, mapped by their identifier
Expand Down Expand Up @@ -83,20 +83,26 @@ async function startWallet(walletId, walletConfig, config, options = {}) {
throw new Error('Invalid parameter for startWallet helper');
}
const hydratedWalletConfig = { ...walletConfig };
const [logger, libLogger] = initializeWalletLogger(walletId);

// Builds the connection object
hydratedWalletConfig.connection = new Connection({
network: config.network,
servers: [config.server],
connectionTimeout: config.connectionTimeout,
logger: libLogger,
});

// tokenUid is optional but if not passed as parameter the wallet will use HTR
if (config.tokenUid) {
hydratedWalletConfig.tokenUid = config.tokenUid;
}

// Set the lib logger to the be wallet logger
hydratedWalletConfig.logger = libLogger;

const wallet = new HathorWallet(hydratedWalletConfig);
setupWalletStateLogs(wallet, logger);

if (options?.historySyncMode || config.history_sync_mode) {
// POLLING_HTTP_API is the default case if something invalid is configured
Expand Down Expand Up @@ -146,7 +152,7 @@ async function startWallet(walletId, walletConfig, config, options = {}) {
Full-node info: ${JSON.stringify(info, null, 2)}`);

initializedWallets.set(walletId, wallet);
initializeWalletLogger(walletId);
walletLoggers.set(walletId, logger);
if (options?.hsmKeyName) {
hsmWalletIds.set(walletId, options.hsmKeyName);
}
Expand Down

0 comments on commit 31d66b1

Please sign in to comment.