Created
October 6, 2020 17:08
-
-
Save james2doyle/18f3a19d7b18ddc7688510b0c10c6658 to your computer and use it in GitHub Desktop.
Nuxt plugin for the Winston logger. Includes setup for logging in the browser console. Written in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Plugin } from '@nuxt/types'; | |
import logger from 'logger'; | |
const eventLogger: Plugin = (context, inject) => { | |
// Inject $logger in Vue, context and store. | |
inject('logger', logger); | |
// For Nuxt <= 2.12, also add 👇 | |
context.$logger = logger; | |
}; | |
export default eventLogger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as winston from 'winston'; | |
import BrowserConsole from 'winston-transport-browserconsole'; | |
const logger = winston.createLogger({ | |
level: 'info', | |
transports: [ | |
new BrowserConsole( | |
{ | |
format: winston.format.simple(), | |
}, | |
), | |
], | |
}); | |
// If we're not in production then log to the `console` with the format: | |
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` | |
// | |
// if (process.env.NODE_ENV !== 'production') { | |
// logger.add(new winston.transports.Console({ | |
// format: winston.format.simple(), | |
// })); | |
// } | |
export default logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment