nuxt-winston-log
A module to add winston
/ logging to your Nuxt application. This module only supports Nuxt apps running in universal mode.
By default the following events are captured:
error
level: SSR errors via Nuxt middleware hooksinfo
level: Basic access logs forserverMiddleware
endpoints + pages in your Nuxt app
All logs captured include a bit of additional metadata pulled from the Node.js request
object:
url: 'https://cool.net' method: 'GET' headers: 'X-Plumbus': "36f7b241-2910-4439-8671-749fc77dc213"
Logs are output at ./logs/{NODE_ENV}.log
by default. They are created in JSON Lines format.
Installation
- Install npm package
$ yarn add nuxt-winston-log # or npm i nuxt-winston-log
- Edit your
nuxt.config.js
file to add module
modules: 'nuxt-winston-log'
- Change options using the
winstonLog
key as needed. See Usage section for details.
Usage
-
By default,
nuxt-winston-log
exposes some basic options for common needs. Internally, these options are used to create a basic Logger instance and wire up middleware.The default values are:
// ...// Path that log files will be created in.// Change this to keep things neat.logPath: './logs'// Name of log file.// Change this to keep things tidy.logName: `.log`// Setting to determine if filesystem is accessed to auto-create logPath.// Set this to `false` for non-filesystem based logging setups.autoCreateLogPath: true// Setting to determine if default logger instance is created for you.// Set this to `false` and provide `loggerOptions` (usage item #3) to// completely customize the logger instance (formatting, transports, etc.)useDefaultLogger: true// Settings to determine if default handlers should be// registered for requests and errors respectively.// Set to `true` to skip request logging (level: info).skipRequestMiddlewareHandler: false// Set to `true` to skip error logging (level: error).skipErrorMiddlewareHandler: false// ... -
To customize the File Transport instance, pass options to the
transportOptions
key:Example in your apps
~/nuxt.config.js
file:const logfilePath = path// Configure nuxt-winston-log modulewinstonLog:transportOptions:filename: logfilePath -
To customize the Logger instance, set
useDefaultLogger
option tofalse
, and make sure you provide a custom set ofloggerOptions
to be passed to Winston'screateLogger
under the hood:Example in your apps
~/nuxt.config.js
file:// Note imports from winston core for transports, formatting helpers, etc.const combine timestamp label prettyPrint = format// Configure nuxt-winston-log modulewinstonLog:useDefaultLogger: falseloggerOptions:format:transports: -
To disable automatic creation of the
logPath
directory, setautoCreateLogPath
option tofalse
:Example in your apps
~/nuxt.config.js
file:// ...// Configure nuxt-winston-log modulewinstonLog:autoCreateLogPath: false// ... -
To access the winston logger instance from within Nuxt lifecycle areas, use the
$winstonLog
key from the Nuxtcontext
object. Note: This is only available for server-side executions. For example, becauseasyncData
is an isomorphic function in Nuxt, you will need to guard$winstonLog
access with something likeif (process.server) { ... }
Example
nuxtServerInit
in your apps~/store/index.js
:// ...const actions =async {$winstonLog}// ...Example
asyncData
in your apps~/pages/somepage.vue
:// ...{if processservercontext$winstonLog}// ... -
To disable default request and error logging behaviors, the
skipRequestMiddlewareHandler
andskipErrorMiddlewareHandler
options can be set totrue
. For more information on what these handlers do out of the box, see the source at the bottom of the~/index.js
file.Example in your apps
~/nuxt.config.js
file:// ...// Configure nuxt-winston-log modulewinstonLog:skipRequestMiddlewareHandler: trueskipErrorMiddlewareHandler: true// ...Adding your own middleware handlers to Nuxt is outside the scope of this documentation, but can be accomplished using a custom module of your own.
Because modules are executed sequentially, your custom module should be loaded after the
nuxt-winston-log
module. You can then access the logger instance viaprocess.winstonLog
as needed.See the
~/index.js
file for some example middleware handlers / hooks.