Skip to content

Commit

Permalink
use winston-embeded format for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JSHan94 committed Jan 2, 2024
1 parent 0341818 commit c77f82b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 56 deletions.
4 changes: 3 additions & 1 deletion bots/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
73 changes: 19 additions & 54 deletions bots/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,58 @@
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',
filename: `logs/${getLogsSubdir()}/${name}_error.log`,
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());
}

return logger;
}

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');
2 changes: 1 addition & 1 deletion bots/src/worker/bridgeExecutor/Resurrector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c77f82b

Please sign in to comment.