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

Fix json log formatting #164

Merged
merged 2 commits into from
Feb 24, 2023
Merged
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
5 changes: 3 additions & 2 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
1 change: 1 addition & 0 deletions zp-relayer/configs/baseConfig.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
7 changes: 6 additions & 1 deletion zp-relayer/services/appLogger.ts
Original file line number Diff line number Diff line change
@@ -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()],
})
24 changes: 12 additions & 12 deletions zp-relayer/utils/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,23 +36,23 @@ 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`)
}
}

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)
Expand All @@ -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)
Expand Down