From ca1114d8f05e1eb0e8c444f43a5d5fa80aae206b Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Fri, 24 Feb 2023 10:25:44 +0400 Subject: [PATCH 1/2] Fix json log formatting --- zp-relayer/configs/baseConfig.ts | 1 + zp-relayer/services/appLogger.ts | 7 ++++++- zp-relayer/utils/web3.ts | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/zp-relayer/configs/baseConfig.ts b/zp-relayer/configs/baseConfig.ts index cc61d429..a3c0da46 100644 --- a/zp-relayer/configs/baseConfig.ts +++ b/zp-relayer/configs/baseConfig.ts @@ -1,6 +1,7 @@ const config = { poolAddress: process.env.COMMON_POOL_ADDRESS as string, startBlock: parseInt(process.env.COMMON_START_BLOCK || '0'), + colorizeLogs: process.env.COMMON_COLORIZE_LOGS === 'true', logLevel: process.env.COMMON_LOG_LEVEL || 'debug', redisUrl: process.env.COMMON_REDIS_URL as string, rpcUrls: (process.env.COMMON_RPC_URL as string).split(' ').filter(url => url.length > 0), diff --git a/zp-relayer/services/appLogger.ts b/zp-relayer/services/appLogger.ts index cb203c61..1c8a629c 100644 --- a/zp-relayer/services/appLogger.ts +++ b/zp-relayer/services/appLogger.ts @@ -1,8 +1,13 @@ import { createLogger, format, transports } from 'winston' import config from '@/configs/baseConfig' +let logFormat = format.combine(format.timestamp(), format.splat(), format.simple()) +if (config.colorizeLogs) { + logFormat = format.combine(format.colorize(), logFormat) +} + export const logger = createLogger({ level: config.logLevel, - format: format.combine(format.colorize(), format.timestamp(), format.splat(), format.simple()), + format: logFormat, transports: [new transports.Console()], }) diff --git a/zp-relayer/utils/web3.ts b/zp-relayer/utils/web3.ts index 36aaee73..a4e4d605 100644 --- a/zp-relayer/utils/web3.ts +++ b/zp-relayer/utils/web3.ts @@ -4,27 +4,27 @@ import { logger } from '@/services/appLogger' export async function getNonce(web3: Web3, address: string) { try { - logger.debug(`Getting transaction count for ${address}`) + logger.debug('Getting transaction count', { address }) const transactionCount = await web3.eth.getTransactionCount(address) - logger.debug(`Transaction count obtained for ${address}: ${transactionCount}`) + logger.debug('Transaction count obtained', { address, transactionCount}) return transactionCount } catch (e) { if (e instanceof Error) logger.error(e.message) - throw new Error(`Nonce cannot be obtained`) + throw new Error('Nonce cannot be obtained') } } export async function getEvents(contract: Contract, event: string, options: PastEventOptions) { try { const contractAddress = contract.options.address - logger.info('%o, Getting past events', { + logger.info('Getting past events', { contractAddress, event, fromBlock: options.fromBlock, toBlock: options.toBlock, }) const pastEvents = await contract.getPastEvents(event, options) - logger.debug('%o, Past events obtained', { + logger.debug('Past events obtained', { contractAddress, event, count: pastEvents.length, @@ -36,15 +36,15 @@ export async function getEvents(contract: Contract, event: string, options: Past } } -export async function getTransaction(web3: Web3, hash: string) { +export async function getTransaction(web3: Web3, txHash: string) { try { - logger.info(`Getting tx ${hash}`) - const tx = await web3.eth.getTransaction(hash) - logger.debug(`Got tx ${hash}`) + logger.info('Getting tx', { txHash }) + const tx = await web3.eth.getTransaction(txHash) + logger.debug('Got tx', { txHash }) return tx } catch (e) { if (e instanceof Error) logger.error(e.message) - throw new Error(`${hash} tx cannot be obtained`) + throw new Error(`${txHash} tx cannot be obtained`) } } @@ -52,7 +52,7 @@ export async function getChainId(web3: Web3) { try { logger.debug('Getting chain id') const chainId = await web3.eth.getChainId() - logger.debug(`Chain id obtained ${chainId}`) + logger.debug('Chain id obtained', { chainId }) return chainId } catch (e) { if (e instanceof Error) logger.error(e.message) @@ -64,7 +64,7 @@ export async function getBlockNumber(web3: Web3) { try { logger.debug('Getting block number') const blockNumber = await web3.eth.getBlockNumber() - logger.debug('Block number obtained %d', blockNumber) + logger.debug('Block number obtained', { blockNumber }) return blockNumber } catch (e) { if (e instanceof Error) logger.error(e.message) From ad1d29248779e9d48022f0173edbc605c0e6a61c Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Fri, 24 Feb 2023 10:31:04 +0400 Subject: [PATCH 2/2] Update CONFIGURATION.md --- CONFIGURATION.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 6c7747bc..3cd4134d 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -5,6 +5,7 @@ These environment variables are required for all services. | name | description | value | | - | - | - | | COMMON_LOG_LEVEL | Log level | Winston log level | +| COMMON_COLORIZE_LOGS | If set to `true`, log levels will be colorized when printed to stdout. | boolean | | COMMON_POOL_ADDRESS | Address of the pool contract | hexadecimal prefixed with "0x" | | COMMON_START_BLOCK | The block number used to start searching for events when the relayer/watcher instance is run for the first time | integer | | COMMON_REDIS_URL | Url to redis instance | URL | @@ -56,5 +57,5 @@ These environment variables are required for all services. | name | description | value | | - | - | - | | WATCHER_EVENT_POLLING_INTERVAL | The interval in milliseconds used to request the RPC node for new blocks. | integer | -| WATCHER_DIRECT_DEPOSIT_BATCH_SIZE | Maximum size of a single direct deposit batch. Defaults to `16`. | integer | -| WATCHER_DIRECT_DEPOSIT_BATCH_TTL | Maximum TTL in milliseconds for a new direct deposit batch. After this time batch will be submitted to the queue, even if it has less than `DIRECT_DEPOSIT_BATCH_SIZE` elements. Defaults to `3600000` (1 hour) | integer | +| DIRECT_DEPOSIT_BATCH_SIZE | Maximum size of a single direct deposit batch. Defaults to `16`. | integer | +| DIRECT_DEPOSIT_BATCH_TTL | Maximum TTL in milliseconds for a new direct deposit batch. After this time batch will be submitted to the queue, even if it has less than `DIRECT_DEPOSIT_BATCH_SIZE` elements. Defaults to `3600000` (1 hour) | integer |