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 for serverMiddleware endpoints + pages in your Nuxt appAll 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.
$ yarn add nuxt-winston-log # or npm i nuxt-winston-log
nuxt.config.js file to add module{
modules: ['nuxt-winston-log']
}
winstonLog key as needed. See Usage section for details.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.// ...
{
// 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: `${process.env.NODE_ENV}.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
}
// ...
transportOptions key:~/nuxt.config.js file:import path from 'path'
const logfilePath = path.resolve(process.cwd(), './logs', `${process.env.NODE_ENV}.log`)
export default {
// Configure nuxt-winston-log module
winstonLog: {
transportOptions: {
filename: logfilePath
}
}
}
useDefaultLogger option to false, and make sure you provide a custom set of loggerOptions to be passed to Winston's createLogger under the hood:~/nuxt.config.js file:// Note imports from winston core for transports, formatting helpers, etc.
import { format, transports } from 'winston'
const { combine, timestamp, label, prettyPrint } = format
export default {
// Configure nuxt-winston-log module
winstonLog: {
useDefaultLogger: false,
loggerOptions: {
format: combine(
label({ label: 'Custom Nuxt logging!' }),
timestamp(),
prettyPrint()
),
transports: [new transports.Console()]
}
}
}
logPath directory, set autoCreateLogPath option to false:~/nuxt.config.js file:// ...
export default {
// Configure nuxt-winston-log module
winstonLog: {
autoCreateLogPath: false
}
}
// ...
$winstonLog key from the Nuxt context object. Note: This is only available for server-side executions. For example, because asyncData is an isomorphic function in Nuxt, you will need to guard $winstonLog access with something like if (process.server) { ... }nuxtServerInit in your apps ~/store/index.js:// ...
export const actions = {
async nuxtServerInit({ store, commit }, { req, $winstonLog }) {
$winstonLog.info(`x-forwarded-host: ${req.headers['x-forwarded-host']}`)
}
}
// ...
asyncData in your apps ~/pages/somepage.vue:// ...
asyncData(context) {
if (process.server) {
context.$winstonLog.info('Hello from asyncData on server')
}
}
// ...
skipRequestMiddlewareHandler and skipErrorMiddlewareHandler options can be set to true. For more information on what these handlers do out of the box, see the source at the bottom of the ~/index.js file.~/nuxt.config.js file:// ...
export default {
// Configure nuxt-winston-log module
winstonLog: {
skipRequestMiddlewareHandler: true,
skipErrorMiddlewareHandler: true
}
}
// ...
nuxt-winston-log module. You can then access the logger instance via process.winstonLog as needed.~/index.js file for some example middleware handlers / hooks.