-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): fix util.format polyfill to accept object as given param…
…eter
- Loading branch information
Showing
36 changed files
with
220 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,4 @@ | |
"peerDependencies": { | ||
"@tsed/logger": "6.7.6" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,4 @@ | |
"peerDependencies": { | ||
"@tsed/logger": "6.7.6" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,4 @@ | |
"peerDependencies": { | ||
"@tsed/logger": "6.7.6" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,4 @@ | |
"peerDependencies": { | ||
"@tsed/logger": "6.7.6" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,5 +50,6 @@ | |
"barrelsby": "^2.8.1", | ||
"typescript": "5.4.2", | ||
"vite": "5.1.6" | ||
} | ||
} | ||
}, | ||
"peerDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
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"); | ||
|
||
$log.appenders.set("console", {type: "console", levels: ["info", "debug", "trace", "fatal", "error", "warn"]}); | ||
|
||
PatternLayout.LayoutReplacer = LayoutReplacer; | ||
StringUtils.format = format; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ | |
*/ | ||
|
||
export * from "./exports"; | ||
export * from "./layouts/LayoutReplacer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/logger/src/common/layouts/components/BasicLayout.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/logger/src/common/layouts/components/MessagePassThroughLayout.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.