diff --git a/bots/src/config.ts b/bots/src/config.ts index 02495912..8f2bfcca 100644 --- a/bots/src/config.ts +++ b/bots/src/config.ts @@ -2,7 +2,9 @@ import { LCDClient } from '@initia/initia.js'; import * as dotenv from 'dotenv'; const envFile = - ( process.env.NODE_ENV === 'test' || !process.env.WORKER_NAME ) ? `.env` : `.env.${process.env.WORKER_NAME}`; + process.env.NODE_ENV === 'test' || !process.env.WORKER_NAME + ? `.env` + : `.env.${process.env.WORKER_NAME}`; console.log('activate ', envFile); dotenv.config({ path: envFile }); diff --git a/bots/src/lib/logger.ts b/bots/src/lib/logger.ts index e919c996..cf9c0d19 100644 --- a/bots/src/lib/logger.ts +++ b/bots/src/lib/logger.ts @@ -1,69 +1,37 @@ import * as winston from 'winston'; import * as DailyRotateFile from 'winston-daily-rotate-file'; -import chalk from 'chalk'; import { config } from 'config'; -function pad(input: number | string, width: number, z = '0') { - const n = typeof input === 'number' ? input.toString() : input; - return n.padStart(width, z); -} - -function getDateString() { - const d = new Date(); - return `${pad(d.getMonth() + 1, 2)}-${pad(d.getDate(), 2)} ${pad( - d.getHours(), - 2 - )}:${pad(d.getMinutes(), 2)}`; -} - function getLogsSubdir() { const d = new Date(); - return `${d.getFullYear()}-${pad(d.getMonth() + 1, 2)}-${pad( - d.getDate(), - 2 - )}`; + return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart( + 2, + '0' + )}-${String(d.getDate()).padStart(2, '0')}`; } function createLogger(name: string) { - const print = winston.format.printf((info) => { - let level; - - if (!config.USE_LOG_FILE) { - // Do not colorize when writing to file - if (info.level === 'error') { - level = chalk.red(info.level.toUpperCase()); - } else if (info.level === 'warn') { - level = chalk.yellow(info.level.toUpperCase()); - } else { - level = chalk.green(info.level.toUpperCase()); - } - } + const formats = [winston.format.errors({ stack: true })]; - const message = info.stack - ? `${info.message}\n${info.stack}` - : info.message; - - const log = `${getDateString()} [${ - level ? level : info.level - } - ${name}]: ${message}`; + if (!config.USE_LOG_FILE) { + formats.push(winston.format.colorize()); + } - return log; - }); + formats.push( + winston.format.timestamp(), + winston.format.printf((info) => { + return `${info.timestamp} [${info.level} - ${name}]: ${ + info.stack || info.message + }`; + }) + ); const logger = winston.createLogger({ - level: 'silly', - format: winston.format.combine( - winston.format.errors({ stack: true, stackTraces: true }), - print - ), + format: winston.format.combine(...formats), defaultMeta: { service: 'user-service' } }); if (config.USE_LOG_FILE) { - // - // - Write to all logs with level `info` and below to `combined.log` - // - Write all logs error (and below) to `error.log`. - // logger.add( new DailyRotateFile({ level: 'error', @@ -71,16 +39,13 @@ function createLogger(name: string) { zippedArchive: true }) ); - logger.add( new DailyRotateFile({ filename: `logs/${getLogsSubdir()}/${name}_combined.log`, zippedArchive: true }) ); - } - - if (!config.USE_LOG_FILE) { + } else { logger.add(new winston.transports.Console()); } @@ -88,6 +53,6 @@ function createLogger(name: string) { } export const executorLogger = createLogger('Executor'); +export const outputLogger = createLogger('Output'); export const batchLogger = createLogger('Batch'); export const challengerLogger = createLogger('Challenger'); -export const outputLogger = createLogger('Output'); diff --git a/bots/src/worker/bridgeExecutor/Resurrector.ts b/bots/src/worker/bridgeExecutor/Resurrector.ts index df2f9723..dae3fa30 100644 --- a/bots/src/worker/bridgeExecutor/Resurrector.ts +++ b/bots/src/worker/bridgeExecutor/Resurrector.ts @@ -46,7 +46,7 @@ export class Resurrector { Buffer.from(failedTx.data, 'hex').toString('base64') ); try { - await this.executor.transaction([msg]) + await this.executor.transaction([msg]); await this.updateProcessed(failedTx); } catch (err) { if (this.errorCounter++ < 20) {