-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6323b7
commit 84b110a
Showing
8 changed files
with
159 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"loglayer": minor | ||
--- | ||
|
||
Add consola support |
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,16 @@ | ||
import { createConsola } from "consola"; | ||
import { LogLayer, LoggerType } from "../src"; | ||
import { testMethods } from "./utils"; | ||
|
||
const consola = createConsola({ | ||
level: 5, | ||
}); | ||
|
||
const log = new LogLayer({ | ||
logger: { | ||
instance: consola, | ||
type: LoggerType.CONSOLA, | ||
}, | ||
}); | ||
|
||
testMethods(log); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { type ConsolaInstance, consola } from "consola"; | ||
import { beforeAll, beforeEach, describe, expect, it, vitest } from "vitest"; | ||
import { LogLayer } from "../LogLayer"; | ||
import { LoggerType } from "../types"; | ||
|
||
let log: LogLayer<ConsolaInstance>; | ||
|
||
describe("structured logger with consola", () => { | ||
beforeAll(() => { | ||
// consola.wrapAll(); | ||
|
||
log = new LogLayer<ConsolaInstance>({ | ||
logger: { | ||
instance: consola, | ||
type: LoggerType.CONSOLA, | ||
}, | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
// Re-mock consola before each test call to remove | ||
// calls from before | ||
consola.mockTypes(() => vitest.fn()); | ||
}); | ||
|
||
it("should log a message", () => { | ||
log.info("this is a test message"); | ||
// @ts-ignore | ||
const consolaMessages = consola.info.mock.calls.map((c) => c[0]); | ||
expect(consolaMessages).toContain("this is a test message"); | ||
}); | ||
|
||
it("should log a message with a prefix", () => { | ||
log.withPrefix("[testing]").info("this is a test message"); | ||
// @ts-ignore | ||
const consolaMessages = consola.info.mock.calls.map((c) => c[0]); | ||
expect(consolaMessages).toContain("[testing] this is a test message"); | ||
}); | ||
|
||
it("should include context", () => { | ||
log.withContext({ | ||
sample: "data", | ||
}); | ||
|
||
log.info("this is a test message"); | ||
// @ts-ignore | ||
const msg = consola.info.mock.calls[0][0]; | ||
expect(msg).toContain("this is a test message"); | ||
// @ts-ignore | ||
const data = consola.info.mock.calls[0][1]; | ||
expect(data).toMatchObject({ | ||
sample: "data", | ||
}); | ||
}); | ||
|
||
it("should include metadata", () => { | ||
log.withContext({ | ||
test: "context", | ||
}); | ||
|
||
log | ||
.withMetadata({ | ||
meta: "data", | ||
}) | ||
.info("this is a test message"); | ||
|
||
// @ts-ignore | ||
const msg = consola.info.mock.calls[0][0]; | ||
expect(msg).toContain("this is a test message"); | ||
// @ts-ignore | ||
const data = consola.info.mock.calls[0][1]; | ||
expect(data).toMatchObject({ | ||
sample: "data", | ||
test: "context", | ||
}); | ||
}); | ||
|
||
it("should include an error", () => { | ||
log.withContext({ | ||
test: "context", | ||
}); | ||
|
||
log.withError(new Error("err")).info("this is a test message"); | ||
// @ts-ignore | ||
const msg = consola.info.mock.calls[0][0]; | ||
expect(msg).toContain("this is a test message"); | ||
// @ts-ignore | ||
const data = consola.info.mock.calls[0][1]; | ||
expect(data).toMatchObject({ | ||
test: "context", | ||
}); | ||
expect(data.err).toBeDefined(); | ||
}); | ||
}); |
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