From 60e4c6242ce0b0f2ac91d93b89e6c7ce7cc17fe3 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 18 Sep 2024 19:06:42 +0200 Subject: [PATCH] fix(logger): fix util.format polyfill to accept object as given parameter --- packages/connect/package.json | 2 +- packages/file/package.json | 2 +- packages/insight/package.json | 2 +- packages/logentries/package.json | 2 +- packages/logger/.barrelsby.json | 14 ++++- packages/logger/package.json | 5 +- packages/logger/src/browser/exports.ts | 4 +- packages/logger/src/browser/index.ts | 1 - .../src/browser/layouts/LayoutReplacer.ts | 2 +- .../logger/src/browser/utils/format.spec.ts | 58 ++++++++++++++++++ packages/logger/src/browser/utils/format.ts | 48 +++++++++++++++ .../components/ConsoleAppender.spec.ts | 4 ++ packages/logger/src/common/index.ts | 5 -- .../layouts/components/BasicLayout.spec.ts | 4 ++ .../common/layouts/components/BasicLayout.ts | 7 ++- .../layouts/components/ColoredLayout.spec.ts | 37 ++++++++++++ .../layouts/components/ColoredLayout.ts | 6 +- .../layouts/components/JsonLayout.spec.ts | 4 ++ .../MessagePassThroughLayout.spec.ts | 4 ++ .../components/MessagePassThroughLayout.ts | 4 +- .../common/layouts/utils/StringUtils.spec.ts | 60 ------------------- .../src/common/layouts/utils/StringUtils.ts | 47 ++------------- .../common/layouts/utils/logEventToObject.ts | 4 +- .../utils/timestampLevelAndCategory.ts | 4 +- .../src/node/appenders/StderrAppender.spec.ts | 4 ++ .../src/node/appenders/StdoutAppender.spec.ts | 4 ++ packages/logger/src/node/exports.ts | 3 + packages/logger/src/node/index.ts | 1 - .../logger/src/node/layouts/LayoutReplacer.ts | 2 +- packages/loggly/package.json | 2 +- packages/logstash-http/package.json | 2 +- packages/logstash-udp/package.json | 2 +- packages/rabbitmq/package.json | 2 +- packages/seq/package.json | 2 +- packages/slack/package.json | 2 +- packages/smtp/package.json | 2 +- 36 files changed, 220 insertions(+), 138 deletions(-) create mode 100644 packages/logger/src/browser/utils/format.spec.ts create mode 100644 packages/logger/src/browser/utils/format.ts delete mode 100644 packages/logger/src/common/layouts/utils/StringUtils.spec.ts diff --git a/packages/connect/package.json b/packages/connect/package.json index 65f50f3d..9899ab5b 100644 --- a/packages/connect/package.json +++ b/packages/connect/package.json @@ -47,4 +47,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/file/package.json b/packages/file/package.json index de5eab3f..d002eef8 100644 --- a/packages/file/package.json +++ b/packages/file/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/insight/package.json b/packages/insight/package.json index dfdefe31..aa4161c1 100644 --- a/packages/insight/package.json +++ b/packages/insight/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/logentries/package.json b/packages/logentries/package.json index 156d7678..ce5c4856 100644 --- a/packages/logentries/package.json +++ b/packages/logentries/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/logger/.barrelsby.json b/packages/logger/.barrelsby.json index b3b5ff2a..3e7bfe65 100644 --- a/packages/logger/.barrelsby.json +++ b/packages/logger/.barrelsby.json @@ -1,5 +1,15 @@ { - "directory": ["./src/common", "./src/browser", "./src/node"], - "exclude": ["__mock__", "__mocks__", ".spec.ts"], + "directory": [ + "./src/common", + "./src/browser", + "./src/node" + ], + "exclude": [ + "__mock__", + "__mocks__", + ".spec.ts", + "utils", + "LayoutReplacer" + ], "delete": true } diff --git a/packages/logger/package.json b/packages/logger/package.json index dbbdefe6..7a057111 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -50,5 +50,6 @@ "barrelsby": "^2.8.1", "typescript": "5.4.2", "vite": "5.1.6" - } -} + }, + "peerDependencies": {} +} \ No newline at end of file diff --git a/packages/logger/src/browser/exports.ts b/packages/logger/src/browser/exports.ts index 12cbf1d8..c13fa8b0 100644 --- a/packages/logger/src/browser/exports.ts +++ b/packages/logger/src/browser/exports.ts @@ -1,5 +1,6 @@ -import {Logger, PatternLayout} from "../common"; +import {Logger, PatternLayout, StringUtils} from "../common"; import {LayoutReplacer} from "./layouts/LayoutReplacer"; +import {format} from "./utils/format"; export * from "../common"; export const $log: Logger = new Logger("default"); @@ -7,3 +8,4 @@ export const $log: Logger = new Logger("default"); $log.appenders.set("console", {type: "console", levels: ["info", "debug", "trace", "fatal", "error", "warn"]}); PatternLayout.LayoutReplacer = LayoutReplacer; +StringUtils.format = format; diff --git a/packages/logger/src/browser/index.ts b/packages/logger/src/browser/index.ts index 5e7b3940..c0fc47e9 100644 --- a/packages/logger/src/browser/index.ts +++ b/packages/logger/src/browser/index.ts @@ -3,4 +3,3 @@ */ export * from "./exports"; -export * from "./layouts/LayoutReplacer"; diff --git a/packages/logger/src/browser/layouts/LayoutReplacer.ts b/packages/logger/src/browser/layouts/LayoutReplacer.ts index a07b4ad3..207d1d0a 100644 --- a/packages/logger/src/browser/layouts/LayoutReplacer.ts +++ b/packages/logger/src/browser/layouts/LayoutReplacer.ts @@ -5,7 +5,7 @@ import {LOG_COLORS} from "../../common/layouts/constants/logColors"; import {colorizeEnd, colorizeStart} from "../../common/layouts/utils/colorizeUtils"; import {IReplacers} from "../../common/layouts/interfaces/Replacers"; import {TokensHandlers} from "../../common/layouts/interfaces/BasicLayoutConfiguration"; -import {format} from "../../common/layouts/utils/StringUtils"; +import {format} from "../utils/format"; export class LayoutReplacer { static EOL = "\n"; diff --git a/packages/logger/src/browser/utils/format.spec.ts b/packages/logger/src/browser/utils/format.spec.ts new file mode 100644 index 00000000..d47f3175 --- /dev/null +++ b/packages/logger/src/browser/utils/format.spec.ts @@ -0,0 +1,58 @@ +import {format} from "./format"; + +describe("format()", () => { + it("should use format string", () => { + const res = format("foo"); + expect(res).toEqual("foo"); + }); + + it("should format string (%s)", () => { + const res = format("%s", "foo"); + expect(res).toEqual("foo"); + }); + + it("should format multiple strings (%s:%s)", () => { + const res = format("%s:%s", "foo", "bar"); + expect(res).toEqual("foo:bar"); + }); + + it("should format digit as NaN (%d)", () => { + const res = format("%d", "foo"); + expect(res).toEqual("NaN"); + }); + + it("should format digit (%d)", () => { + const res = format("%d", "16"); + expect(res).toEqual("16"); + }); + + it("should format json string (%j)", () => { + const res = format("%j", "foo"); + expect(res).toEqual('"foo"'); + }); + + it("should format json array (%j)", () => { + const res = format("%j", [1, 2, 3]); + expect(res).toEqual("[1,2,3]"); + }); + + it("should format object string (%o)", () => { + const res = format("%o", "foo"); + expect(res).toEqual("foo"); + }); + + it("should format error (%o)", () => { + const res = format("%o", new Error("mock error")); + expect(res).toEqual("Error: mock error"); + }); + + it("should format object array (%o)", () => { + const res = format("%o", [1, 2, 3]); + expect(res).toEqual("[1,2,3]"); + }); + + it("should format escaped string (%s)", () => { + const res = format("%%s", "foo"); + expect(res).toEqual("%s foo"); + }); +}); diff --git a/packages/logger/src/browser/utils/format.ts b/packages/logger/src/browser/utils/format.ts new file mode 100644 index 00000000..1d31c2b2 --- /dev/null +++ b/packages/logger/src/browser/utils/format.ts @@ -0,0 +1,48 @@ +export function format(fmt: string | object, ...args: unknown[]) { + const re = /(%?)(%([ojds]))/g; + + if (typeof fmt === "object") { + fmt = JSON.stringify(fmt, null, 2); + } + + if (args.length) { + const replacer = (match: any, escaped: any, ptn: any, flag: any) => { + let arg = args.shift(); + switch (flag) { + case "o": + if (Array.isArray(arg)) { + arg = JSON.stringify(arg); + break; + } + case "s": + arg = "" + arg; + break; + case "d": + arg = Number(arg); + break; + case "j": + arg = JSON.stringify(arg); + break; + } + + if (!escaped) { + return arg; + } + + args.unshift(arg); + return match; + }; + + fmt = String(fmt).replace(re, replacer); + } + + // arguments remain after formatting + if (args.length) { + fmt += " " + args.join(" "); + } + + // update escaped %% values + fmt = String(fmt).replace(/%{2,2}/g, "%"); + + return "" + fmt; +} diff --git a/packages/logger/src/common/appenders/components/ConsoleAppender.spec.ts b/packages/logger/src/common/appenders/components/ConsoleAppender.spec.ts index 12f25636..b0f5f08e 100644 --- a/packages/logger/src/common/appenders/components/ConsoleAppender.spec.ts +++ b/packages/logger/src/common/appenders/components/ConsoleAppender.spec.ts @@ -2,6 +2,10 @@ import {ConsoleAppender} from "./ConsoleAppender"; import {LogEvent} from "../../core/LogEvent"; import {levels} from "../../core/LogLevel"; import "../../layouts/components/ColoredLayout"; +import {StringUtils} from "../../layouts/utils/StringUtils"; +import {format} from "node:util"; + +StringUtils.format = format; describe("ConsoleAppender", () => { it("should log something", () => { diff --git a/packages/logger/src/common/index.ts b/packages/logger/src/common/index.ts index 58bce2b9..07e3fff5 100644 --- a/packages/logger/src/common/index.ts +++ b/packages/logger/src/common/index.ts @@ -24,10 +24,5 @@ export * from "./layouts/decorators/layout"; export * from "./layouts/interfaces/BasicLayoutConfiguration"; export * from "./layouts/interfaces/Replacers"; export * from "./layouts/registries/LayoutsRegistry"; -export * from "./layouts/utils/StringUtils"; -export * from "./layouts/utils/colorizeUtils"; -export * from "./layouts/utils/logEventToObject"; -export * from "./layouts/utils/timestampLevelAndCategory"; export * from "./logger/class/Logger"; export * from "./logger/class/LoggerAppenders"; -export * from "./logger/utils/tableUtils"; diff --git a/packages/logger/src/common/layouts/components/BasicLayout.spec.ts b/packages/logger/src/common/layouts/components/BasicLayout.spec.ts index 5ca597b1..00cdea5a 100644 --- a/packages/logger/src/common/layouts/components/BasicLayout.spec.ts +++ b/packages/logger/src/common/layouts/components/BasicLayout.spec.ts @@ -1,6 +1,10 @@ import {LogEvent} from "../../core/LogEvent"; import {levels} from "../../core/LogLevel"; import {BasicLayout} from "./BasicLayout"; +import {StringUtils} from "../utils/StringUtils"; +import {format} from "node:util"; + +StringUtils.format = format; describe("BasicLayout", () => { it("should return a formated string", () => { diff --git a/packages/logger/src/common/layouts/components/BasicLayout.ts b/packages/logger/src/common/layouts/components/BasicLayout.ts index aad4db54..d9da6117 100644 --- a/packages/logger/src/common/layouts/components/BasicLayout.ts +++ b/packages/logger/src/common/layouts/components/BasicLayout.ts @@ -2,7 +2,7 @@ import {timestampLevelAndCategory} from "../utils/timestampLevelAndCategory"; import {BaseLayout} from "../class/BaseLayout"; import {LogEvent} from "../../core/LogEvent"; import {Layout} from "../decorators/layout"; -import {format} from "../utils/StringUtils"; +import {StringUtils} from "../utils/StringUtils"; @Layout({name: "basic"}) export class BasicLayout extends BaseLayout { @@ -16,6 +16,9 @@ export class BasicLayout extends BaseLayout { * @author Stephan Strittmatter */ transform(loggingEvent: LogEvent, timezoneOffset?: number): string { - return timestampLevelAndCategory(loggingEvent, undefined, timezoneOffset) + (format as any)(...[].concat(loggingEvent.data as any)); + return ( + timestampLevelAndCategory(loggingEvent, undefined, timezoneOffset) + + (StringUtils.format as any)(...[].concat(loggingEvent.data as any)) + ); } } diff --git a/packages/logger/src/common/layouts/components/ColoredLayout.spec.ts b/packages/logger/src/common/layouts/components/ColoredLayout.spec.ts index b618b468..3522f27d 100644 --- a/packages/logger/src/common/layouts/components/ColoredLayout.spec.ts +++ b/packages/logger/src/common/layouts/components/ColoredLayout.spec.ts @@ -1,6 +1,10 @@ import {LogEvent} from "../../core/LogEvent"; import {levels} from "../../core/LogLevel"; import {ColoredLayout} from "./ColoredLayout"; +import {StringUtils} from "../utils/StringUtils"; +import {format} from "node:util"; + +StringUtils.format = format; describe("ColoredLayout", () => { it("should return a formatted string", () => { @@ -18,4 +22,37 @@ describe("ColoredLayout", () => { expect(result).toEqual(expect.stringContaining("[36m[2017-06-18T22:29:38.234] [DEBUG] [category] - data")); }); + + it("should return a formatted string from object", () => { + const layout = new ColoredLayout({ + type: "colored" + }); + + const context = new Map(); + context.set("user", "romain"); + + const logEvent = new LogEvent( + "category", + levels().DEBUG, + [ + { + id: "id", + event: "event", + headers: { + accept: "application/json" + } + } + ], + context as any + ); + (logEvent as any)._startTime = new Date("2017-06-18 22:29:38.234"); + + const result = layout.transform(logEvent); + + expect(result).toEqual( + expect.stringContaining( + "[2017-06-18T22:29:38.234] [DEBUG] [category] - { id: 'id', event: 'event', headers: { accept: 'application/json' } }" + ) + ); + }); }); diff --git a/packages/logger/src/common/layouts/components/ColoredLayout.ts b/packages/logger/src/common/layouts/components/ColoredLayout.ts index b9c4e6dc..d52d5456 100644 --- a/packages/logger/src/common/layouts/components/ColoredLayout.ts +++ b/packages/logger/src/common/layouts/components/ColoredLayout.ts @@ -3,7 +3,7 @@ import {timestampLevelAndCategory} from "../utils/timestampLevelAndCategory"; import {LOG_COLORS} from "../constants/logColors"; import {Layout} from "../decorators/layout"; import {BaseLayout} from "../class/BaseLayout"; -import {format} from "../utils/StringUtils"; +import {StringUtils} from "../utils/StringUtils"; @Layout({name: "colored"}) export class ColoredLayout extends BaseLayout { @@ -14,6 +14,8 @@ export class ColoredLayout extends BaseLayout { transform(loggingEvent: LogEvent, timezoneOffset?: number): string { const index: any = loggingEvent.level.toString(); const color = LOG_COLORS[index as keyof typeof LOG_COLORS]; - return timestampLevelAndCategory(loggingEvent, color, timezoneOffset) + (format as any)(...[].concat(loggingEvent.data as any)); + return ( + timestampLevelAndCategory(loggingEvent, color, timezoneOffset) + (StringUtils.format as any)(...[].concat(loggingEvent.data as any)) + ); } } diff --git a/packages/logger/src/common/layouts/components/JsonLayout.spec.ts b/packages/logger/src/common/layouts/components/JsonLayout.spec.ts index f44e59c3..5d673b15 100644 --- a/packages/logger/src/common/layouts/components/JsonLayout.spec.ts +++ b/packages/logger/src/common/layouts/components/JsonLayout.spec.ts @@ -2,6 +2,10 @@ import {LogEvent} from "../../core/LogEvent"; import {levels} from "../../core/LogLevel"; import {JsonLayout} from "./JsonLayout"; import {LogContext} from "../../core/LogContext"; +import {StringUtils} from "../utils/StringUtils"; +import {format} from "node:util"; + +StringUtils.format = format; describe("JsonLayout", () => { describe("when separator is given", () => { diff --git a/packages/logger/src/common/layouts/components/MessagePassThroughLayout.spec.ts b/packages/logger/src/common/layouts/components/MessagePassThroughLayout.spec.ts index 7d47fa12..7dd00458 100644 --- a/packages/logger/src/common/layouts/components/MessagePassThroughLayout.spec.ts +++ b/packages/logger/src/common/layouts/components/MessagePassThroughLayout.spec.ts @@ -1,6 +1,10 @@ import {LogEvent} from "../../core/LogEvent"; import {levels} from "../../core/LogLevel"; import {MessagePassThroughLayout} from "./MessagePassThroughLayout"; +import {StringUtils} from "../utils/StringUtils"; +import {format} from "node:util"; + +StringUtils.format = format; describe("MessagePassThroughLayout", () => { it("should return a formatted string", () => { diff --git a/packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts b/packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts index 09cd3722..a35a05e2 100644 --- a/packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts +++ b/packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts @@ -1,12 +1,12 @@ import {BaseLayout} from "../class/BaseLayout"; import {LogEvent} from "../../core/LogEvent"; import {Layout} from "../decorators/layout"; -import {format} from "../utils/StringUtils"; +import {StringUtils} from "../utils/StringUtils"; @Layout({name: "messagePassThrough"}) export class MessagePassThroughLayout extends BaseLayout { transform(loggingEvent: LogEvent, timezoneOffset?: number): string { // @ts-ignore - return format(...[].concat(loggingEvent.data as any)); + return StringUtils.format(...[].concat(loggingEvent.data as any)); } } diff --git a/packages/logger/src/common/layouts/utils/StringUtils.spec.ts b/packages/logger/src/common/layouts/utils/StringUtils.spec.ts deleted file mode 100644 index cbdc2499..00000000 --- a/packages/logger/src/common/layouts/utils/StringUtils.spec.ts +++ /dev/null @@ -1,60 +0,0 @@ -import {format} from "./StringUtils"; - -describe("StringUtils", () => { - describe("format()", () => { - it("should use format string", () => { - const res = format("foo"); - expect(res).toEqual("foo"); - }); - - it("should format string (%s)", () => { - const res = format("%s", "foo"); - expect(res).toEqual("foo"); - }); - - it("should format multiple strings (%s:%s)", () => { - const res = format("%s:%s", "foo", "bar"); - expect(res).toEqual("foo:bar"); - }); - - it("should format digit as NaN (%d)", () => { - const res = format("%d", "foo"); - expect(res).toEqual("NaN"); - }); - - it("should format digit (%d)", () => { - const res = format("%d", "16"); - expect(res).toEqual("16"); - }); - - it("should format json string (%j)", () => { - const res = format("%j", "foo"); - expect(res).toEqual('"foo"'); - }); - - it("should format json array (%j)", () => { - const res = format("%j", [1, 2, 3]); - expect(res).toEqual("[1,2,3]"); - }); - - it("should format object string (%o)", () => { - const res = format("%o", "foo"); - expect(res).toEqual("foo"); - }); - - it("should format error (%o)", () => { - const res = format("%o", new Error("mock error")); - expect(res).toEqual("Error: mock error"); - }); - - it("should format object array (%o)", () => { - const res = format("%o", [1, 2, 3]); - expect(res).toEqual("[1,2,3]"); - }); - - it("should format escaped string (%s)", () => { - const res = format("%%s", "foo"); - expect(res).toEqual("%s foo"); - }); - }); -}); diff --git a/packages/logger/src/common/layouts/utils/StringUtils.ts b/packages/logger/src/common/layouts/utils/StringUtils.ts index 802c071b..4317b1ca 100644 --- a/packages/logger/src/common/layouts/utils/StringUtils.ts +++ b/packages/logger/src/common/layouts/utils/StringUtils.ts @@ -35,47 +35,8 @@ export function truncateAndPad(toTruncAndPad: string, truncation: string, paddin return replacement; } -export function format(fmt: string, ...args: unknown[]) { - const re = /(%?)(%([ojds]))/g; - - if (args.length) { - const replacer = (match: any, escaped: any, ptn: any, flag: any) => { - let arg = args.shift(); - switch (flag) { - case "o": - if (Array.isArray(arg)) { - arg = JSON.stringify(arg); - break; - } - case "s": - arg = "" + arg; - break; - case "d": - arg = Number(arg); - break; - case "j": - arg = JSON.stringify(arg); - break; - } - - if (!escaped) { - return arg; - } - - args.unshift(arg); - return match; - }; - - fmt = String(fmt).replace(re, replacer); +export const StringUtils = { + format(fmt: string | object, ...args: any[]): string { + return ""; } - - // arguments remain after formatting - if (args.length) { - fmt += " " + args.join(" "); - } - - // update escaped %% values - fmt = String(fmt).replace(/%{2,2}/g, "%"); - - return "" + fmt; -} +}; diff --git a/packages/logger/src/common/layouts/utils/logEventToObject.ts b/packages/logger/src/common/layouts/utils/logEventToObject.ts index ed735c8d..e47092b6 100644 --- a/packages/logger/src/common/layouts/utils/logEventToObject.ts +++ b/packages/logger/src/common/layouts/utils/logEventToObject.ts @@ -1,6 +1,6 @@ import {LogEvent} from "../../core/LogEvent"; import {removeColors} from "./colorizeUtils"; -import {format} from "./StringUtils"; +import {StringUtils} from "./StringUtils"; export function logEventToObject(loggingEvent: LogEvent) { const log: any = { @@ -21,7 +21,7 @@ export function logEventToObject(loggingEvent: LogEvent) { return data; } - return [...data, removeColors(format(current))]; + return [...data, removeColors(StringUtils.format(current))]; }, []); return log; diff --git a/packages/logger/src/common/layouts/utils/timestampLevelAndCategory.ts b/packages/logger/src/common/layouts/utils/timestampLevelAndCategory.ts index 6af180a8..7a5edbc3 100644 --- a/packages/logger/src/common/layouts/utils/timestampLevelAndCategory.ts +++ b/packages/logger/src/common/layouts/utils/timestampLevelAndCategory.ts @@ -2,11 +2,11 @@ import * as dateFormat from "date-format"; import {colorize} from "./colorizeUtils"; import {LogEvent} from "../../core/LogEvent"; -import {format} from "./StringUtils"; +import {StringUtils} from "./StringUtils"; export function timestampLevelAndCategory(loggingEvent: LogEvent, colour: any, timezoneOffset: number | undefined) { return colorize( - format( + StringUtils.format( "[%s] [%s] [%s] - ", dateFormat.asString(loggingEvent.startTime, timezoneOffset), loggingEvent.formattedLevel, diff --git a/packages/logger/src/node/appenders/StderrAppender.spec.ts b/packages/logger/src/node/appenders/StderrAppender.spec.ts index 26c048ba..329e3a76 100644 --- a/packages/logger/src/node/appenders/StderrAppender.spec.ts +++ b/packages/logger/src/node/appenders/StderrAppender.spec.ts @@ -1,5 +1,9 @@ import {StderrAppender} from "./StderrAppender"; import {levels, LogEvent} from "../../common"; +import {format} from "node:util"; +import {StringUtils} from "../../common/layouts/utils/StringUtils"; + +StringUtils.format = format; describe("StderrAppender", () => { it("should log something", () => { diff --git a/packages/logger/src/node/appenders/StdoutAppender.spec.ts b/packages/logger/src/node/appenders/StdoutAppender.spec.ts index 161ebab3..2ce3542a 100644 --- a/packages/logger/src/node/appenders/StdoutAppender.spec.ts +++ b/packages/logger/src/node/appenders/StdoutAppender.spec.ts @@ -2,6 +2,10 @@ import {levels, LogEvent} from "../../common"; import {StdoutAppender} from "./StdoutAppender"; import "./StdoutAppender"; import "./StdoutAppender"; +import {format} from "node:util"; +import {StringUtils} from "../../common/layouts/utils/StringUtils"; + +StringUtils.format = format; describe("StdoutAppender", () => { it("should log something", () => { diff --git a/packages/logger/src/node/exports.ts b/packages/logger/src/node/exports.ts index 2697a4fc..4887b589 100644 --- a/packages/logger/src/node/exports.ts +++ b/packages/logger/src/node/exports.ts @@ -1,7 +1,9 @@ +import {format} from "node:util"; import {Logger, PatternLayout} from "../common/index"; import "./appenders/StdoutAppender"; import "./appenders/StderrAppender"; import {LayoutReplacer} from "./layouts/LayoutReplacer"; +import {StringUtils} from "../common/layouts/utils/StringUtils"; export const $log: Logger = new Logger("default"); @@ -10,3 +12,4 @@ $log.appenders .set("stderr", {type: "stderr", levels: ["trace", "fatal", "error", "warn"]}); PatternLayout.LayoutReplacer = LayoutReplacer; +StringUtils.format = format; diff --git a/packages/logger/src/node/index.ts b/packages/logger/src/node/index.ts index 2f6ebdee..79213d47 100644 --- a/packages/logger/src/node/index.ts +++ b/packages/logger/src/node/index.ts @@ -5,4 +5,3 @@ export * from "./exports"; export * from "./appenders/StderrAppender"; export * from "./appenders/StdoutAppender"; -export * from "./layouts/LayoutReplacer"; diff --git a/packages/logger/src/node/layouts/LayoutReplacer.ts b/packages/logger/src/node/layouts/LayoutReplacer.ts index ea27761d..da653ddd 100644 --- a/packages/logger/src/node/layouts/LayoutReplacer.ts +++ b/packages/logger/src/node/layouts/LayoutReplacer.ts @@ -1,3 +1,4 @@ +import Util from "node:util"; // @ts-ignore import * as dateFormat from "date-format"; import {LogEvent} from "../../common/core/LogEvent"; @@ -6,7 +7,6 @@ import {colorizeEnd, colorizeStart} from "../../common/layouts/utils/colorizeUti import {IReplacers} from "../../common/layouts/interfaces/Replacers"; import {TokensHandlers} from "../../common/layouts/interfaces/BasicLayoutConfiguration"; import Os from "os"; -import Util from "util"; export class LayoutReplacer { static EOL = Os.EOL || "\n"; diff --git a/packages/loggly/package.json b/packages/loggly/package.json index 8f7005ad..52db37d1 100644 --- a/packages/loggly/package.json +++ b/packages/loggly/package.json @@ -49,4 +49,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/logstash-http/package.json b/packages/logstash-http/package.json index 3eea2c82..8b16a643 100644 --- a/packages/logstash-http/package.json +++ b/packages/logstash-http/package.json @@ -52,4 +52,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/logstash-udp/package.json b/packages/logstash-udp/package.json index 4fe8a8e8..eee6cfaf 100644 --- a/packages/logstash-udp/package.json +++ b/packages/logstash-udp/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/rabbitmq/package.json b/packages/rabbitmq/package.json index 4cc16651..c97f546e 100644 --- a/packages/rabbitmq/package.json +++ b/packages/rabbitmq/package.json @@ -49,4 +49,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/seq/package.json b/packages/seq/package.json index be7e0e8f..1778c792 100644 --- a/packages/seq/package.json +++ b/packages/seq/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/slack/package.json b/packages/slack/package.json index b6d6aa6c..350defc9 100644 --- a/packages/slack/package.json +++ b/packages/slack/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file diff --git a/packages/smtp/package.json b/packages/smtp/package.json index 08ca0940..2faf705b 100644 --- a/packages/smtp/package.json +++ b/packages/smtp/package.json @@ -48,4 +48,4 @@ "peerDependencies": { "@tsed/logger": "6.7.6" } -} +} \ No newline at end of file