useLogger
The useLogger composable provides a flexible, adapter-based logging system for Vue applications. It supports multiple log levels, runtime enable/disable toggling, and integrates seamlessly with popular logging libraries like Consola and Pino through a simple adapter pattern.
Installation
Install the Logger plugin in your app’s entry point:
main.ts
import { createApp } from 'vue'
import { createLoggerPlugin } from '@vuetify/v0'
import App from './App.vue'
const app = createApp(App)
app.use(
createLoggerPlugin({
level: 'debug',
prefix: '[MyApp]',
})
)
app.mount('#app')Usage
Once the plugin is installed, use the useLogger composable in any component:
vue
<script setup lang="ts">
import { useLogger } from '@vuetify/v0'
const logger = useLogger()
logger.info('Component mounted')
logger.debug('Debug information', { userId: 123 })
logger.warn('Warning message')
logger.error('Error occurred', new Error('Something went wrong'))
</script>
<template>
<div>
<h1>Check the console for logs</h1>
</div>
</template>Architecture
useLogger uses the plugin pattern with a log adapter:
Reactivity
The logger uses internal reactive state for level and enabled, but exposes only functions. There are no reactive properties — all interactions are through imperative methods.
The following API details are for the useLogger composable.
Functions
createLoggerContext
<_E>(_options?: LoggerContextOptions | undefined) => ContextTrinity<_E>createLoggerPlugin
(_options?: LoggerContextOptions | undefined) => PluginuseLogger
<_E>(namespace?: string) => _EOptions
adapter
LoggerAdapter | undefinedlevel
LogLevel | undefinedprefix
string | undefinedenabled
boolean | undefinedMethods
debug
(message: string, ...args: unknown[]) => voidinfo
(message: string, ...args: unknown[]) => voidwarn
(message: string, ...args: unknown[]) => voiderror
(message: string, ...args: unknown[]) => voidtrace
(message: string, ...args: unknown[]) => voidfatal
(message: string, ...args: unknown[]) => voidlevel
(level: LogLevel) => voidcurrent
() => LogLevelenabled
() => booleanenable
() => voiddisable
() => voidWas this page helpful?