-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kit): allow customising logger options (#24243)
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 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
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 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { consola } from 'consola' | ||
import { logger, useLogger } from './logger' | ||
|
||
vi.mock("consola", () => { | ||
const logger = {} as any; | ||
|
||
logger.create = vi.fn(() => ({...logger})); | ||
logger.withTag = vi.fn(() => ({...logger})); | ||
|
||
return { consola: logger }; | ||
}) | ||
|
||
describe('logger', () => { | ||
it('should expose consola', () => { | ||
expect(logger).toBe(consola) | ||
}) | ||
}) | ||
|
||
describe('useLogger', () => { | ||
it('should expose consola when not passing a tag', () => { | ||
expect(useLogger()).toBe(consola); | ||
}); | ||
|
||
it('should create a new instance when passing a tag', () => { | ||
const logger = vi.mocked(consola); | ||
|
||
const instance = useLogger("tag"); | ||
|
||
expect(instance).toEqual(logger); | ||
expect(instance).not.toBe(logger); | ||
expect(logger.create).toBeCalledWith({}); | ||
expect(logger.withTag).toBeCalledWith("tag"); | ||
}); | ||
|
||
it('should create a new instance when passing a tag and options', () => { | ||
const logger = vi.mocked(consola); | ||
|
||
const instance = useLogger("tag", { level: 0 }); | ||
|
||
expect(instance).toEqual(logger); | ||
expect(instance).not.toBe(logger); | ||
expect(logger.create).toBeCalledWith({ level: 0 }); | ||
expect(logger.withTag).toBeCalledWith("tag"); | ||
}); | ||
|
||
}) |
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,7 +1,8 @@ | ||
import { consola } from 'consola' | ||
import type { ConsolaOptions } from 'consola'; | ||
|
||
export const logger = consola | ||
|
||
export function useLogger (tag?: string) { | ||
return tag ? logger.withTag(tag) : logger | ||
export function useLogger (tag?: string, options: Partial<ConsolaOptions> = {}) { | ||
return tag ? logger.create(options).withTag(tag) : logger | ||
} |