Logging

Source
Nuxt Kit provides a set of utilities to help you work with logging. These functions allow you to log messages with extra features.

Nuxt provides a logger instance that you can use to log messages with extra features. useLogger allows you to get a logger instance.

useLogger

Returns a logger instance. It uses consola under the hood.

Usage

import { defineNuxtModule, useLogger } from '@nuxt/kit'

export default defineNuxtModule({
  setup (options, nuxt) {
    const logger = useLogger('my-module')

    logger.info('Hello from my module!')
  },
})

Type

function useLogger (tag?: string, options?: NuxtLoggerOptions): NuxtLogger

Parameters

tag: A tag to suffix all log messages with, displayed on the right near the timestamp.

options: Logger options, such as level, reporters, defaults and formatOptions.

Example

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!')
  },
})

useTerminal

Returns a set of primitives for interacting with the user's terminal. When Nuxt is running inside an interactive host such as the nuxt dev terminal UI, prompts, tasks and notifications are handed to it, so they are answerable and rendered in one place. Otherwise they fall back to logging.

Usage

declare function runPackageInstall (name: string): Promise<void>
// ---cut---
import { defineNuxtModule, useTerminal } from '@nuxt/kit'

export default defineNuxtModule({
  async setup () {
    const terminal = useTerminal()

    const install = await terminal.prompt('Do you want to install `my-module`?', { type: 'confirm' })
    if (!install) {
      return
    }

    const task = terminal.startTask('Installing `my-module`...')
    // run your actual task
    await runPackageInstall('my-module')
    task.stop('Installed `my-module`')
  },
})

Type

function useTerminal (): NuxtTerminal

interface NuxtTerminal {
  readonly interactive: boolean
  withTerminal: <T>(work: () => Promise<T>) => Promise<T>
  prompt: (message: string, options?: NuxtPromptOptions) => Promise<any>
  startTask: (label: string) => NuxtTerminalTask
  notify: (notification: NuxtTerminalNotification) => NuxtTerminalNotice
}

Properties

interactive: Whether an interactive host is present. When false, the primitives below log to the current process streams instead.

withTerminal: Borrows the terminal for the duration of work, suspending any host UI and releasing stdin. Use it when you need to write to the terminal or read from stdin directly. Concurrent callers are serialised, and a nested call from within a borrow runs immediately.

prompt: Asks the user a question, borrowing the terminal for as long as the prompt is open. Takes the same options as logger.prompt.

startTask: Starts a long-running task, rendered on the host's status surface where available. Finish it with task.stop(message?, outcome?), or change its label with task.update(label).

notify: Shows a message and holds it on screen until the user acknowledges it or notice.dismiss() is called. notice.dismissed settles once the notice is gone.