A small but helpful logging module for nuxt 3 projects based on nuxt-logger created by mahnuh
- Add
nuxt-log4nuxt
dependency to your project
# Using pnpm
pnpm add -D nuxt-log4nuxt
# Using yarn
yarn add --dev nuxt-log4nuxt
# Using npm
npm install --save-dev nuxt-log4nuxt
- Add
nuxt-log4nuxt
to themodules
section ofnuxt.config.ts
export default defineNuxtConfig({
modules: [
'nuxt-log4nuxt'
],
// OPTIONAL CONFIGURATION
log4nuxt: {
isEnabled: true, // true or false, defaults to true
logLevel: 'trace', // trace, debug, info, warn or error, defaults to debug
}
})
That's it! You can now use log4nuxt in your Nuxt app ✨
There are 5 methods available, one for each log level (trace
, debug
, info
, warn
, error
) which accept one or more arguments of any kind.
For example
this.$log4nuxt.trace('log a trace message')
this.$log4nuxt.debug('log a debug message')
this.$log4nuxt.info('log an info message')
this.$log4nuxt.warn('log a warn message')
this.$log4nuxt.error('log an error message')
Some more advanced examples of accessing the module methods are given below:
import { useNuxtApp } from '#app'
import { defineComponent } from '@vue/composition-api'
export default defineComponent({
setup() {
useNuxtApp().$log4nuxt.debug('logging on setup')
},
data() {
this.$log4nuxt.debug('logging from the data method')
return {
hello: "Hello World!"
}
},
mounted() {
console.log("logging without log4nuxt")
this.$log4nuxt.debug('logging from mounted')
this.callLogger(this.$log4nuxt)
},
methods: {
callLogger(logger : any) {
logger.trace('logging trace from a method')
logger.debug('logging debug from a method')
logger.info('logging info from a method')
logger.warn('logging warn from a method')
logger.error('logging error from a method')
}
}
})