Skip to content

Commit

Permalink
feat(kit): allow customising logger options (#24243)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniAcu authored and manniL committed Dec 11, 2023
1 parent bd916da commit 8870f36
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
22 changes: 21 additions & 1 deletion docs/3.api/5.kit/13.logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Returns a logger instance. It uses [consola](https://github.com/unjs/consola) un
### Type

```ts
function useLogger (tag?: string): ConsolaInstance
function useLogger (tag?: string, options?: Partial<ConsolaOptions>): ConsolaInstance
```

### Parameters
Expand All @@ -30,6 +30,14 @@ function useLogger (tag?: string): ConsolaInstance

A tag to prefix all log messages with.

#### `options`

**Type**: `Partial<ConsolaOptions>`

***Optional**: `true`

Consola configuration options

### Examples

```ts
Expand All @@ -43,3 +51,15 @@ export default defineNuxtModule({
}
})
```

```ts
import { defineNuxtModule, useLogger } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const logger = useLogger('my-module', { level: options.quiet ? 0 : 3 })
logger.info('Hello from my module!')
}
})
```
48 changes: 48 additions & 0 deletions packages/kit/src/logger.test.ts
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");
});

})
5 changes: 3 additions & 2 deletions packages/kit/src/logger.ts
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
}

0 comments on commit 8870f36

Please sign in to comment.