From c33ec782ba123717330a841b92c7bd3d80490733 Mon Sep 17 00:00:00 2001 From: shuse2 <shus.toda@gmail.com> Date: Mon, 19 Aug 2024 11:27:09 +0200 Subject: [PATCH 1/3] feat: update log format --- src/utils/LogUtils.ts | 36 ++++++++++++++++++++++++++++++++++++ src/utils/index.ts | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/utils/LogUtils.ts b/src/utils/LogUtils.ts index b84973e957..5178a3f8db 100644 --- a/src/utils/LogUtils.ts +++ b/src/utils/LogUtils.ts @@ -1 +1,37 @@ +// Modify from: https://github.com/UMAprotocol/protocol/blob/master/packages/logger/src/logger/ConsoleTransport.ts +// Fix to console log in single line, and add error message to log. +import winston from "winston"; +import { createNewLogger } from "@uma/logger"; +// This transport enables Winston logging to the console. +const { format } = winston; +const { combine, timestamp, colorize, printf } = format; + +export function createConsoleTransport(): winston.transports.ConsoleTransportInstance { + return new winston.transports.Console({ + handleExceptions: true, + format: combine( + // Adds timestamp. + colorize(), + timestamp(), + printf((info) => { + const { timestamp, level, error, ...args } = info; + + // This slice changes a timestamp formatting from `2020-03-25T10:50:57.168Z` -> `2020-03-25 10:50:57` + const ts = timestamp.slice(0, 19).replace("T", " "); + // Winston does not properly log Error objects like console.error() does, so this formatter will search for the Error object + // in the "error" property of "info", and add the error stack to the log. + // Discussion at https://github.com/winstonjs/winston/issues/1338. + if (error) { + args.errMsg = error; + } + const log = `${ts} [${level}]: ${Object.keys(args).length ? JSON.stringify(args) : ""}`; + + return log; + }) + ), + }); +} + +export const Logger = createNewLogger([createConsoleTransport()], { createConsoleTransport: false }); + export type DefaultLogLevels = "debug" | "info" | "warn" | "error"; diff --git a/src/utils/index.ts b/src/utils/index.ts index 6ff007d2da..e5fa9e34a1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -29,7 +29,7 @@ export type { Block, TransactionResponse, TransactionReceipt, Provider } from "@ export { config } from "dotenv"; export { replaceAddressCase } from "@uma/common"; -export { Logger, waitForLogger } from "@uma/logger"; +export { waitForLogger } from "@uma/logger"; export { CHAIN_IDs, From 131525961a1f5e5f2acb3bca3089584056e81e36 Mon Sep 17 00:00:00 2001 From: shuse2 <shus.toda@gmail.com> Date: Mon, 19 Aug 2024 12:16:14 +0200 Subject: [PATCH 2/3] feat: update to log fully in JSON --- src/utils/LogUtils.ts | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/src/utils/LogUtils.ts b/src/utils/LogUtils.ts index 5178a3f8db..ad351f3ded 100644 --- a/src/utils/LogUtils.ts +++ b/src/utils/LogUtils.ts @@ -1,37 +1,9 @@ -// Modify from: https://github.com/UMAprotocol/protocol/blob/master/packages/logger/src/logger/ConsoleTransport.ts // Fix to console log in single line, and add error message to log. import winston from "winston"; -import { createNewLogger } from "@uma/logger"; -// This transport enables Winston logging to the console. -const { format } = winston; -const { combine, timestamp, colorize, printf } = format; +import { createNewLogger, createConsoleTransport } from "@uma/logger"; -export function createConsoleTransport(): winston.transports.ConsoleTransportInstance { - return new winston.transports.Console({ - handleExceptions: true, - format: combine( - // Adds timestamp. - colorize(), - timestamp(), - printf((info) => { - const { timestamp, level, error, ...args } = info; +const transports = process.env.DEBUG_LOG === "true" ? [createConsoleTransport()] : [new winston.transports.Console()]; - // This slice changes a timestamp formatting from `2020-03-25T10:50:57.168Z` -> `2020-03-25 10:50:57` - const ts = timestamp.slice(0, 19).replace("T", " "); - // Winston does not properly log Error objects like console.error() does, so this formatter will search for the Error object - // in the "error" property of "info", and add the error stack to the log. - // Discussion at https://github.com/winstonjs/winston/issues/1338. - if (error) { - args.errMsg = error; - } - const log = `${ts} [${level}]: ${Object.keys(args).length ? JSON.stringify(args) : ""}`; - - return log; - }) - ), - }); -} - -export const Logger = createNewLogger([createConsoleTransport()], { createConsoleTransport: false }); +export const Logger = createNewLogger(transports, { createConsoleTransport: false }); export type DefaultLogLevels = "debug" | "info" | "warn" | "error"; From e7241bb9fa8e0dc8de798f75959022c04af0da1c Mon Sep 17 00:00:00 2001 From: shuse2 <shus.toda@gmail.com> Date: Mon, 19 Aug 2024 12:17:36 +0200 Subject: [PATCH 3/3] bug: fix config --- src/utils/LogUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/LogUtils.ts b/src/utils/LogUtils.ts index ad351f3ded..346e19138d 100644 --- a/src/utils/LogUtils.ts +++ b/src/utils/LogUtils.ts @@ -3,7 +3,8 @@ import winston from "winston"; import { createNewLogger, createConsoleTransport } from "@uma/logger"; const transports = process.env.DEBUG_LOG === "true" ? [createConsoleTransport()] : [new winston.transports.Console()]; +const config = process.env.DEBUG_LOG === "true" ? {} : { createConsoleTransport: false }; -export const Logger = createNewLogger(transports, { createConsoleTransport: false }); +export const Logger = createNewLogger(transports, config); export type DefaultLogLevels = "debug" | "info" | "warn" | "error";