diff --git a/.node-version b/.node-version new file mode 100644 index 0000000..2bd5a0a --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +22 diff --git a/.nvmrc b/.nvmrc deleted file mode 100644 index 3c03207..0000000 --- a/.nvmrc +++ /dev/null @@ -1 +0,0 @@ -18 diff --git a/build.js b/build.js deleted file mode 100644 index 232d821..0000000 --- a/build.js +++ /dev/null @@ -1,14 +0,0 @@ -// import { build } from "esbuild"; - -// build({ -// entryPoints: ["src/index.ts"], -// outfile: "dist/browser/index.js", -// platform: "browser", -// bundle: true, -// minify: true, -// format: "iife", -// globalName: "tslog", -// loader: { ".ts": "ts" }, -// }) -// .then(() => console.log("⚡ Done")) -// .catch(() => process.exit(1)); diff --git a/build.mjs b/build.mjs new file mode 100644 index 0000000..469e263 --- /dev/null +++ b/build.mjs @@ -0,0 +1,14 @@ +import { build } from "esbuild"; + +build({ + entryPoints: ["src/index.ts"], + outfile: "dist/browser/index.js", + platform: "browser", + bundle: true, + minify: true, + format: "iife", + globalName: "timbergeist", + loader: { ".ts": "ts" }, +}) + .then(() => console.log("⚡ Done")) + .catch(() => process.exit(1)); diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..6a0493c --- /dev/null +++ b/mise.toml @@ -0,0 +1,2 @@ +[tools] +node = "22" diff --git a/package.json b/package.json index 8cf835a..e6e32db 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "types": "./dist/types/index.d.ts", "scripts": { "build": "npm run build-types && npm run build-server && npm run build-browser", - "build-browser": "node build.js", + "build-browser": "node build.mjs", "build-types": "tsc -b tsconfig.types.json", "build-server": "tsc -b tsconfig.cjs.json && cp ./src/package-cjs.json ./dist/cjs/package.json", "build-example": "tsc -b tsconfig.example.json", @@ -19,7 +19,7 @@ "dev-js": "npm run build-example && node --experimental-specifier-resolution=node --enable-source-maps examples/dist/examples/nodejs/index2.js", "lint": "eslint --ext .js,.ts .", "format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"", - "test": "JEST_PUPPETEER_CONFIG=jest-puppeteer.config.cjs jest && yarn ts-node tests/RAW/stack_trace.test.ts", + "test": "JEST_PUPPETEER_CONFIG=jest-puppeteer.config.cjs jest", "test-puppeteer-serve": "npm run build-browser && node tests/Browser/server/index.cjs -p 4444", "coverage": "JEST_PUPPETEER_CONFIG=jest-puppeteer.config.cjs jest --coverage", "docsify-init": "docsify init ./docs", @@ -30,7 +30,7 @@ "release:npm": "yarn prerelease && yarn publish --no-git-tag-version --access public" }, "engines": { - "node": ">=16" + "node": ">=18" }, "jest": { "verbose": true, diff --git a/tests/Browser/1_json.test.ts b/tests/Browser/1_json.test.ts index 193b169..a26762e 100644 --- a/tests/Browser/1_json.test.ts +++ b/tests/Browser/1_json.test.ts @@ -19,27 +19,10 @@ describe("Browser: JSON: Log level", () => { await expect(html).toContain("tslog Demo"); }); - it("silly", async () => { - await page.evaluate(() => { - // @ts-ignore - const logger = new tslog.Logger({ type: "json" }); - logger.silly("Test"); - }); - - expect(consoleOutput).toContain('"0":"Test"'); - expect(consoleOutput).toContain('"_meta":{'); - expect(consoleOutput).toContain('"runtime":"Browser"'); - expect(consoleOutput).toContain(`"date":"${new Date().toISOString().split(".")[0]}`); // ignore ms - expect(consoleOutput).toContain('"logLevelId":0'); - expect(consoleOutput).toContain('"logLevelName":"SILLY"'); - expect(consoleOutput).toContain('"path":{'); - expect(consoleOutput).toContain('"fileLine":"22"'); - }); - it("pretty", async () => { await page.evaluate(() => { // @ts-ignore - const logger = new tslog.Logger({ type: "pretty" }); + const logger = new timbergeist.Logger({ type: "pretty" }); logger.silly("Test"); }); @@ -49,7 +32,7 @@ describe("Browser: JSON: Log level", () => { it("pretty no styles", async () => { await page.evaluate(() => { // @ts-ignore - const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false }); + const logger = new timbergeist.Logger({ type: "pretty", stylePrettyLogs: false }); logger.silly("Test"); }); @@ -59,7 +42,7 @@ describe("Browser: JSON: Log level", () => { it("pretty no styles undefined", async () => { await page.evaluate(() => { // @ts-ignore - const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false }); + const logger = new timbergeist.Logger({ type: "pretty", stylePrettyLogs: false }); logger.fatal("Test undefined", { test: undefined }); }); @@ -69,7 +52,7 @@ describe("Browser: JSON: Log level", () => { it("pretty string interpolation", async () => { await page.evaluate(() => { // @ts-ignore - const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false }); + const logger = new timbergeist.Logger({ type: "pretty", stylePrettyLogs: false }); logger.info("Foo %s", "bar"); }); diff --git a/tests/Nodejs/11_Transports.test.ts b/tests/Nodejs/11_Transports.test.ts index 8e9b346..f6273f7 100644 --- a/tests/Nodejs/11_Transports.test.ts +++ b/tests/Nodejs/11_Transports.test.ts @@ -9,9 +9,13 @@ describe("Transports", () => { transports.push(logObj); }}); - const logMsg = logger.info("string", 0, { test: 123 }); + logger.info("string", 0, { test: 123 }); - expect(logMsg).toMatchObject(transports[0]); + expect(transports[0]).toMatchObject({ + "0": "string", + "1": 0, + "2": { test: 123 }, + }); }); test("attach two transport", (): void => { @@ -24,9 +28,17 @@ describe("Transports", () => { transports.push(logObj); }}); - const logMsg = logger.info("string", 0, { test: 123 }); + logger.info("string", 0, { test: 123 }); - expect(logMsg).toMatchObject(transports[0]); - expect(logMsg).toMatchObject(transports[1]); + expect(transports[0]).toMatchObject({ + "0": "string", + "1": 0, + "2": { test: 123 }, + }); + expect(transports[1]).toMatchObject({ + "0": "string", + "1": 0, + "2": { test: 123 }, + }); }); }); diff --git a/tests/Nodejs/12_SubLoggers_and_Prefixes.test.ts b/tests/Nodejs/12_SubLoggers_and_Prefixes.test.ts index fed10b1..a62ed78 100644 --- a/tests/Nodejs/12_SubLoggers_and_Prefixes.test.ts +++ b/tests/Nodejs/12_SubLoggers_and_Prefixes.test.ts @@ -3,45 +3,35 @@ import { Logger } from "../../src/index.js"; describe("SubLoggers", () => { test("one sub logger", (): void => { + const transports: any[] = []; const mainLogger = new Logger(); - const logMsg = mainLogger.info("main logger"); - expect(logMsg?.["0"]).toBe("main logger"); + mainLogger.attachTransport({ transport: (logObj) => { + transports.push(logObj); + }}); + mainLogger.info("main logger"); + expect(transports[0]).toMatchObject({ + "0": "main logger", + }); const subLogger = mainLogger.getSubLogger(); - const subLogMsg = subLogger.info("sub logger"); - expect(subLogMsg?.["0"]).toBe("sub logger"); + subLogger.info("sub logger"); + expect(transports[1]).toMatchObject({ + "0": "sub logger", + }); }); test("one sub logger with prefix", (): void => { - const mainLogger = new Logger({ type: "hidden", prefix: ["main"] }); - const logMsg = mainLogger.info("test-main"); - expect(logMsg?.["0"]).toBe("main"); - expect(logMsg?.["1"]).toBe("test-main"); + const loggerNames: any[] = []; + const mainLogger = new Logger({ name: "main" }); + mainLogger.attachTransport({ transport: (logArgs, logMeta) => { + loggerNames.push(logMeta); + }}); + mainLogger.info("test-main"); + expect(loggerNames[0].name).toBe("main"); - const subLogger = mainLogger.getSubLogger({ type: "hidden", prefix: ["sub"] }); - const subLogMsg = subLogger.info("test-sub"); - expect(subLogMsg?.["0"]).toBe("main"); - expect(subLogMsg?.["1"]).toBe("sub"); - expect(subLogMsg?.["2"]).toBe("test-sub"); - }); - - test("sub logger overwriting LogObj", (): void => { - const mainLogObj = { - main: true, - sub: false, - }; - const mainLogger = new Logger({ type: "hidden" }, mainLogObj); - const logMsg = mainLogger.info("main logger"); - expect(logMsg?.main).toBe(true); - expect(logMsg?.sub).toBe(false); - - const subLogObj = { - main: false, - sub: true, - }; - const subLogger = mainLogger.getSubLogger({ type: "hidden" }, subLogObj); - const subLogMsg = subLogger.info("test-sub"); - expect(subLogMsg?.main).toBe(false); - expect(subLogMsg?.sub).toBe(true); + const subLogger = mainLogger.getSubLogger({ name: "sub" }); + subLogger.info("test-sub"); + expect(loggerNames[1].name).toBe("sub"); + expect(loggerNames[1].parentNames).toContain("main"); }); }); diff --git a/tests/Nodejs/13_Recursive.test.ts b/tests/Nodejs/13_Recursive.test.ts index 5816803..6d08ec3 100644 --- a/tests/Nodejs/13_Recursive.test.ts +++ b/tests/Nodejs/13_Recursive.test.ts @@ -1,52 +1,14 @@ import "ts-jest"; import { Logger } from "../../src/index.js"; -import { mockConsoleLog } from "./helper.js"; +import { getConsoleOutput, mockConsoleLog } from "./helper.js"; describe("Recursive", () => { beforeEach(() => { mockConsoleLog(true, false); }); - test("hidden", (): void => { - const mainLogger = new Logger({ type: "hidden" }); - - /* - * Circular example - * */ - function Foo() { - /* @ts-ignore */ - this.abc = "Hello"; - /* @ts-ignore */ - this.circular = this; - } - /* @ts-ignore */ - const foo = new Foo(); - const logMsg = mainLogger.info("circular", foo); - expect(logMsg?.["0"]).toBe("circular"); - expect(logMsg?.["1"]["circular"]).toEqual(logMsg?.["1"]); - }); - - test("json", (): void => { - const mainLogger = new Logger({ type: "json" }); - - /* - * Circular example - * */ - function Foo() { - /* @ts-ignore */ - this.abc = "Hello"; - /* @ts-ignore */ - this.circular = this; - } - /* @ts-ignore */ - const foo = new Foo(); - const logMsg = mainLogger.info("circular", foo); - expect(logMsg?.["0"]).toBe("circular"); - expect(logMsg?.["1"]["circular"]).toEqual(logMsg?.["1"]); - }); - test("pretty", (): void => { - const mainLogger = new Logger({ type: "pretty" }); + const mainLogger = new Logger(); /* * Circular example @@ -59,26 +21,8 @@ describe("Recursive", () => { } /* @ts-ignore */ const foo = new Foo(); - const logMsg = mainLogger.info("circular", foo); - expect(logMsg?.["0"]).toBe("circular"); - expect(logMsg?.["1"]["circular"]).toEqual(logMsg?.["1"]); - }); - - test("pretty recursive LogObj function", (): void => { - /* - * Circular example - * */ - function Foo() { - /* @ts-ignore */ - this.abc = "Hello"; - /* @ts-ignore */ - this.circular = this; - } - /* @ts-ignore */ - const foo = new Foo(); - const mainLogger = new Logger({ type: "pretty" }, foo); - - const logMsg = mainLogger.info("circular"); - expect(logMsg?.["0"]).toBe("circular"); + mainLogger.info("circular", foo); + expect(getConsoleOutput()).toContain("circular"); + expect(getConsoleOutput()).toContain("Hello"); }); }); diff --git a/tests/Nodejs/14_Getters_Setters.test.ts b/tests/Nodejs/14_Getters_Setters.test.ts index 5c8fd15..5ed57c2 100644 --- a/tests/Nodejs/14_Getters_Setters.test.ts +++ b/tests/Nodejs/14_Getters_Setters.test.ts @@ -1,5 +1,4 @@ import "ts-jest"; -import { ok } from "assert"; import { Logger } from "../../src/index.js"; import { getConsoleOutput, mockConsoleLog } from "./helper.js"; @@ -22,23 +21,15 @@ describe("Getters and setters", () => { test("[class] should not print getters on class instance (prototype)", (): void => { // Node.js issue: https://github.com/nodejs/node/issues/30183 - const logger = new Logger({ - type: "pretty", - }); + const logger = new Logger(); const missingSetterObj = new MissingSetter(); - const result = logger.info(missingSetterObj); - ok(result); - expect(Object.keys(result)).not.toContain("testProp"); + logger.info(missingSetterObj); expect(getConsoleOutput()).not.toContain("testProp"); }); test("[object] should print getters", (): void => { - const logger = new Logger({ - type: "pretty", - }); - const result = logger.info(missingSetter); - ok(result); - expect(Object.keys(result)).toContain("testProp"); + const logger = new Logger(); + logger.info(missingSetter); expect(getConsoleOutput()).toContain("testProp"); }); }); diff --git a/tests/Nodejs/5_pretty_Log_Types.test.ts b/tests/Nodejs/5_pretty_Log_Types.test.ts index 6886815..3b3d20b 100644 --- a/tests/Nodejs/5_pretty_Log_Types.test.ts +++ b/tests/Nodejs/5_pretty_Log_Types.test.ts @@ -74,7 +74,7 @@ describe("Pretty: Log Types", () => { }); test("Object, String", (): void => { - const logger = new Logger(); + const logger = new Logger({}, [new PrettyPrinterTransport(ConsoleSink, { stylePrettyLogs: false })]); logger.log(1234, "testLevel", { test: true, nested: { 1: false } }, "test"); expect(getConsoleOutput()).toContain("{ test: true, nested: { '1': false } } test"); }); diff --git a/tests/Nodejs/7_pretty_Settings.test.ts b/tests/Nodejs/7_pretty_Settings.test.ts index ea04ddb..dedaaea 100644 --- a/tests/Nodejs/7_pretty_Settings.test.ts +++ b/tests/Nodejs/7_pretty_Settings.test.ts @@ -1,5 +1,5 @@ import "ts-jest"; -import { Logger } from "../../src/index.js"; +import { ILogObjMeta, Logger } from "../../src/index.js"; import { getConsoleOutput, mockConsoleLog } from "./helper.js"; describe("Pretty: Settings", () => { @@ -8,41 +8,49 @@ describe("Pretty: Settings", () => { }); test("plain string", (): void => { - const logger = new Logger({ type: "pretty" }); + const logger = new Logger(); logger.log(1234, "testLevel", "Test"); expect(getConsoleOutput()).toContain("testLevel"); expect(getConsoleOutput()).toContain("Test"); }); test("two strings", (): void => { - const logger = new Logger({ type: "pretty" }); + const logger = new Logger(); logger.log(1234, "testLevel", "Test1", "Test2"); expect(getConsoleOutput()).toContain("Test1 Test2"); }); test("name", (): void => { - const logger = new Logger({ type: "pretty", name: "logger" }); - const log = logger.log(1, "testLevel", "foo bar"); - expect(log).toBeDefined(); - expect(log?._meta?.name).toBe("logger"); + const logsMeta: ILogObjMeta[] = []; + const logger = new Logger({ name: "logger" }); + logger.attachTransport({ transport: (logArgs, logMeta) => { + logsMeta.push(logMeta); + }}); + logger.log(1, "testLevel", "foo bar"); + expect(logsMeta[0]).toBeDefined(); + expect(logsMeta[0].name).toBe("logger"); expect(getConsoleOutput()).toContain(`logger`); }); test("name with sub-logger inheritance", (): void => { - const logger1 = new Logger({ type: "pretty", name: "logger1" }); + const logsMeta: ILogObjMeta[] = []; + const logger1 = new Logger({ name: "logger1" }); const logger2 = logger1.getSubLogger({ name: "logger2" }); const logger3 = logger2.getSubLogger({ name: "logger3" }); - const log1 = logger1.log(1, "testLevel", "foo bar"); - const log2 = logger2.log(1, "testLevel", "foo bar"); - const log3 = logger3.log(1, "testLevel", "foo bar"); - expect(log1).toBeDefined(); - expect(log2).toBeDefined(); - expect(log3).toBeDefined(); + logger1.attachTransport({ transport: (logArgs, logMeta) => { + logsMeta.push(logMeta); + }}); - expect(log1?._meta?.name).toBe("logger1"); - expect(log2?._meta?.name).toBe("logger2"); - expect(log3?._meta?.name).toBe("logger3"); + logger1.log(1, "testLevel", "foo bar"); + logger2.log(1, "testLevel", "foo bar"); + logger3.log(1, "testLevel", "foo bar"); + + expect(logsMeta.length).toBe(3); + + expect(logsMeta[0].name).toBe("logger1"); + expect(logsMeta[1].name).toBe("logger2"); + expect(logsMeta[2].name).toBe("logger3"); expect(getConsoleOutput()).toContain(`logger1`); expect(getConsoleOutput()).toContain(`logger1:logger2`); @@ -51,343 +59,27 @@ describe("Pretty: Settings", () => { test("argumentsArray", (): void => { const logger = new Logger({ - type: "pretty", argumentsArrayName: "argumentsArray", }); logger.log(1234, "testLevel", "Test1", "Test2"); expect(getConsoleOutput()).toContain("Test1 Test2"); }); - test("hideLogPositionForProduction", (): void => { - const loggerNormal = new Logger({ - type: "pretty", - hideLogPositionForProduction: false, - stylePrettyLogs: false, - }); - const loggerProduction = new Logger({ - type: "pretty", - hideLogPositionForProduction: true, - stylePrettyLogs: false, - }); - - loggerNormal.log(1234, "testLevel", "Normal log"); - loggerProduction.log(1234, "testLevel", "Production log"); - expect(getConsoleOutput()).toContain("testLevel\t/tests/Nodejs/7_pretty_Settings.test.ts:73\tNormal log"); - expect(getConsoleOutput()).toContain("testLevel\t\tProduction log"); - }); - test("metaProperty", (): void => { - const logger = new Logger({ type: "pretty", metaProperty: "_test" }); + const logger = new Logger({ defaultMetadata: { _test: "test" } }); logger.log(1234, "testLevel", "Test"); expect(getConsoleOutput()).toContain("Test"); }); - test("Don't mask", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: [], - }); - logger.log(1234, "testLevel", { - password: "pass123", - otherKey: "otherKey456", - nested: { - moviePassword: "swordfish", - }, - }); - - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).toContain("otherKey456"); - expect(getConsoleOutput()).toContain("moviePassword:"); - expect(getConsoleOutput()).toContain("swordfish"); - }); - - test("maskValuesOfKeys not set", (): void => { - const logger = new Logger({ type: "pretty" }); - logger.log(1234, "testLevel", { - password: "pass123", - }); - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("'[***]'"); - expect(getConsoleOutput()).not.toContain("pass123"); - }); - - test("maskValuesOfKeys set and maskPlaceholder", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: ["otherKey"], - maskPlaceholder: "[###]", - }); - logger.log(1234, "testLevel", { - password: "pass123", - otherKey: "otherKey456", - }); - - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - }); - - test("maskValuesOfKeys set two keys and maskPlaceholder", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: ["password", "otherKey", "yetanotherKey"], - maskPlaceholder: "[###]", - }); - logger.log(1234, "testLevel", { - password: "pass123", - otherKey: "otherKey456", - yetAnotherKey: "otherKey789", - }); - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("[###]"); - expect(getConsoleOutput()).not.toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - expect(getConsoleOutput()).toContain("yetAnotherKey:"); - expect(getConsoleOutput()).toContain("otherKey789"); - }); - - test("maskValuesOfKeys set and maskPlaceholder nested object", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: ["otherKey", "moviePassword"], - maskPlaceholder: "[###]", - }); - logger.log(1234, "testLevel", { - password: "pass123", - otherKey: "otherKey456", - nested: { - moviePassword: "swordfish", - }, - }); - - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("[###]"); - expect(getConsoleOutput()).toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - expect(getConsoleOutput()).toContain("moviePassword:"); - expect(getConsoleOutput()).not.toContain("swordfish"); - }); - - test("maskValuesOfKeys and maskValuesOfKeysCaseInsensitive", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: ["password", "otherkey"], - maskValuesOfKeysCaseInsensitive: true, - }); - logger.log(1234, "testLevel", { - password: "pass123", - otherKey: "otherKey456", - }); - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("[***]"); - expect(getConsoleOutput()).not.toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - }); - - test("maskValuesOfKeys and don't manipulate original", (): void => { - const logger = new Logger({ - type: "pretty", - maskValuesOfKeys: ["password", "otherkey"], - maskValuesOfKeysCaseInsensitive: true, - }); - const obj = { - password: "pass123", - otherKey: "otherKey456", - }; - logger.log(1234, "testLevel", obj); - expect(getConsoleOutput()).toContain("password:"); - expect(getConsoleOutput()).toContain("[***]"); - expect(getConsoleOutput()).not.toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey:"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - expect(obj.password).toBe("pass123"); - expect(obj.otherKey).toBe("otherKey456"); - }); - - test("maskValuesRegEx with different types and without manipulating the original object", (): void => { - const logger = new Logger({ - type: "pretty", - stylePrettyLogs: false, - maskValuesRegEx: [new RegExp("otherKey", "g")], - }); - - const logObj = { - password: "pass123", - otherKey: "otherKey456", - }; - - logger.log(1234, "testLevel", logObj); - - expect(getConsoleOutput()).toContain("password: '[***]'"); - expect(getConsoleOutput()).not.toContain("pass123"); - expect(getConsoleOutput()).toContain("otherKey: '[***]456'"); - expect(getConsoleOutput()).not.toContain("otherKey456"); - - logger.log(4567, "testLevel", undefined); - expect(getConsoleOutput()).toContain("undefined"); - logger.log(4567, "testLevel", "string"); - expect(getConsoleOutput()).toContain("string"); - logger.log(4567, "testLevel", 0); - expect(getConsoleOutput()).toContain("0"); - logger.log(4567, "testLevel", NaN); - expect(getConsoleOutput()).toContain("NaN"); - logger.log(4567, "testLevel", { object: true }); - expect(getConsoleOutput()).toContain(`{ - object: 'true' -}`); - logger.log(4567, "testLevel", new Date()); - expect(getConsoleOutput()).toContain("T"); - expect(getConsoleOutput()).toContain("Z"); - logger.log(4567, "testLevel", Buffer.from("foo")); - expect(getConsoleOutput()).toContain(" { - const logger = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}** ", - stylePrettyLogs: false, - }); - logger.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().replace("T", " ").split(".")[0]}`); - expect(getConsoleOutput()).toContain("** Test"); - }); - - test("stylePrettyLogs: false / prettyLogTemplate - no shortcut: {{dd}}.{{mm}}.{{yyyy}} {{hh}}:{{MM}}", (): void => { - const logger = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{dd}}.{{mm}}.{{yyyy}} {{hh}}:{{MM}}** ", - stylePrettyLogs: false, - }); - logger.log(1234, "testLevel", "Test"); - const yyyy = new Date().getUTCFullYear(); - const dateMonth = new Date().getUTCMonth(); - const mm = dateMonth == null ? "--" : dateMonth < 9 ? "0" + (dateMonth + 1) : dateMonth + 1; - const dateDay = new Date().getUTCDate(); - const dd = dateDay == null ? "--" : dateDay < 10 ? "0" + dateDay : dateDay; - const dateHours = new Date().getUTCHours(); - const hh = dateHours == null ? "--" : dateHours < 10 ? "0" + dateHours : dateHours; - const dateMinutes = new Date().getUTCMinutes(); - const MM = dateMinutes == null ? "--" : dateMinutes < 10 ? "0" + dateMinutes : dateMinutes; - expect(getConsoleOutput()).toContain(`**${dd}.${mm}.${yyyy} ${hh}:${MM}** Test`); - }); - - test("stylePrettyLogs: false / prettyLogTemplate - shortcut: {{dateIsoStr}}", (): void => { - const logger = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{dateIsoStr}}** ", - stylePrettyLogs: false, - }); - logger.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().replace("T", " ").replace("Z", "").split(".")[0]}`); - expect(getConsoleOutput()).toContain("** Test"); - }); - - test("prettyLogTemplate - rawIsoStr", (): void => { - const logger = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{rawIsoStr}}** ", - stylePrettyLogs: false, - }); - logger.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().split(".")[0]}`); - expect(getConsoleOutput()).toContain("** Test"); - }); - - test("prettyLogTimeZone - rawIsoStr - UTC (default)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{rawIsoStr}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().split(".")[0]}`); - }); - - test("prettyLogTimeZone - rawIsoStr - UTC (configured)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTimeZone: "UTC", - prettyLogTemplate: "**{{rawIsoStr}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().split(".")[0]}`); - }); - - test("prettyLogTimeZone - rawIsoStr - local (configured)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTimeZone: "local", - prettyLogTemplate: "**{{rawIsoStr}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toISOString().split(".")[0]}`); - }); - - test("prettyLogTimeZone - {{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}} - UTC (default)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTemplate: "**{{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().split(".")[0]}`); - }); - - test("prettyLogTimeZone - {{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}} - UTC (configured)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTimeZone: "UTC", - prettyLogTemplate: "**{{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date().toISOString().split(".")[0]}`); - }); - - test("prettyLogTimeZone - {{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}} - local (configured)", (): void => { - const loggerShortcut = new Logger({ - type: "pretty", - prettyLogTimeZone: "local", - prettyLogTemplate: "**{{yyyy}}-{{mm}}-{{dd}}T{{hh}}:{{MM}}:{{ss}}** ", - stylePrettyLogs: false, - }); - - loggerShortcut.log(1234, "testLevel", "Test"); - expect(getConsoleOutput()).toContain(`**${new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toISOString().split(".")[0]}`); - }); - test("Change settings: minLevel", (): void => { const logger = new Logger({ - type: "pretty", minLevel: 1, }); logger.log(1, "custom_level_one", "LOG1"); logger.log(2, "custom_level_two", "LOG2"); // change minLevel to 2 - logger.settings.minLevel = 2; + logger.options.minLevel = 2; logger.log(1, "custom_level_one", "LOG3"); logger.log(2, "custom_level_two", "LOG4"); diff --git a/tests/Nodejs/9_pretty_Styles.test.ts b/tests/Nodejs/9_pretty_Styles.test.ts deleted file mode 100644 index ed3974c..0000000 --- a/tests/Nodejs/9_pretty_Styles.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -import "ts-jest"; -import { Logger } from "../../src/index.js"; -import { getConsoleOutput, mockConsoleLog } from "./helper.js"; - -describe("Pretty: Styles", () => { - beforeEach(() => { - mockConsoleLog(true, false); - }); - - test("Logger with default styles", (): void => { - const logger = new Logger({ type: "pretty", stylePrettyLogs: true }); - logger.silly("Test silly"); - expect(getConsoleOutput()).toContain("\u001b[37m\u001b[1mSILLY\u001b[22m\u001b[39m"); - logger.warn("Test warn"); - expect(getConsoleOutput()).toContain("\u001b[33m\u001b[1mWARN\u001b[22m\u001b[39m"); - }); - - test("Logger with changed styles", (): void => { - const logger = new Logger({ - type: "pretty", - stylePrettyLogs: true, - prettyLogStyles: { - logLevelName: { - "*": ["bold", "dim"], - SILLY: ["bold", "blue"], - WARN: ["bold", "green"], - }, - }, - }); - logger.silly("Test silly"); - expect(getConsoleOutput()).toContain("\u001b[34m\u001b[1mSILLY\u001b[22m\u001b[39m"); - logger.warn("Test warn"); - expect(getConsoleOutput()).toContain("\u001b[32m\u001b[1mWARN\u001b[22m\u001b[39m"); - logger.log(0, "unknown", "Test unknown"); - expect(getConsoleOutput()).toContain("\u001b[2m\u001b[1munknown\u001b[22m\u001b[22m"); - }); - - test("Logger with missing style", (): void => { - const logger = new Logger({ - type: "pretty", - stylePrettyLogs: true, - prettyLogStyles: { - logLevelName: {}, - }, - }); - logger.log(0, "unknown", "Test unknown"); - expect(getConsoleOutput()).toContain("unknown"); - }); -}); diff --git a/tests/Raw/stack_trace.test.ts b/tests/Raw/stack_trace.test.ts deleted file mode 100644 index 406b91d..0000000 --- a/tests/Raw/stack_trace.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { ConsoleSink, Logger, PrettyPrinterTransport, Sink } from "../../src" - - -const sink: Sink = (formattedString: string, logLevelId: number) => { - ConsoleSink(formattedString, logLevelId); - - if (/tests\/Raw\/stack_trace\.test\.(js|ts):\d+?/g.test(formattedString)) { - return; - } - - throw new Error("Formatted string does not match the expected pattern."); -} - -const logger = new Logger(undefined, [ - new PrettyPrinterTransport(sink, {}) -]) - -logger.info("stack_trace.test passed");