admin管理员组文章数量:1357387
Winston is a logging framework that we want to use in our Nuxt3 application, but the Winston package from Nuxt is still only available for Nuxt2. There is sparse documentation on getting this particular bination of tools working.
I tried pulling in the Nuxt Winston package anyway (), but that did not work as expected.
Note that this is only for logging in Nuxt's SSR functionality ("server-side rendering"). I'm not trying to get Winston to log from the client side.
Winston is a logging framework that we want to use in our Nuxt3 application, but the Winston package from Nuxt is still only available for Nuxt2. There is sparse documentation on getting this particular bination of tools working.
I tried pulling in the Nuxt Winston package anyway (https://www.npmjs./package/nuxt-winston-log), but that did not work as expected.
Note that this is only for logging in Nuxt's SSR functionality ("server-side rendering"). I'm not trying to get Winston to log from the client side.
Share Improve this question edited Jul 13, 2023 at 18:39 CJ314 asked Jul 13, 2023 at 18:12 CJ314CJ314 2113 silver badges7 bronze badges3 Answers
Reset to default 9So in this case, we just want to use Winston directly:
Step 1
Run yarn add winston
Step 2
Create a server-side plugin. Prefix the file with 00.
since plugins are loaded in alphabetical order, and you'll likely want your logging available in your other plugins. Suffix with .server.ts
so that this only tries to run on the server side; Winston isn't meant for use on client side.
Filename: plugins/00.logging.server.ts
import { defineNuxtPlugin } from "#app";
import winston from "winston";
export default defineNuxtPlugin(() => {
const logger = winston.createLogger({
level: 'info',
format: winston.format.bine(winston.format.printf((event) => {
return `${JSON.stringify({ ...event, timestamp: new Date().toISOString() }, null, 4)}\n`;
})),
defaultMeta: { service: 'myappname' },
transports: [
new winston.transports.Console()
],
});
return {
provide: {
logger
}
}
});
Step 3 (Optional)
For ease of use and so that a single logging function can be called from both the frontend and the backend, build some posables.
Filename: posables/logging.ts
export function logServerError(err: any, message?: string) {
const nuxtApp = useNuxtApp();
if (process?.server && nuxtApp.$logger) {
nuxtApp.$logger.log("error", message, err);
} else {
console.error("We Have Encountered an ERROR: ", err);
}
}
export function logServerWarn(message: string) {
const nuxtApp = useNuxtApp();
if (process?.server && nuxtApp.$logger) {
nuxtApp.$logger.log("warn", message);
} else {
console.warn(message);
}
}
export function logServerInfo(message: string) {
const nuxtApp = useNuxtApp();
if (process?.server && nuxtApp.$logger) {
nuxtApp.$logger.log("info", message);
} else {
console.info(message);
}
}
Step 4 (If you did Step 3)
Use the logger. Filename: plugins/50.myplugin.js
export default defineNuxtPlugin(async (nuxtApp) => {
// do stuff to setup my app
// do more stuff
logServerInfo("Plugin operations plete");
});
Notes
- You might get TS warnings on the references to
nuxtApp.$logger.log
. After all this rigamarole, I just put// @ts-ignore
and moved on. - The filename of the plugin doesn't matter if none of your other plugins needs logging.
- You have to
provide
the logger itself from the plugin, not the logger.log function - If you want different output (say to a file instead of the console), reference the Winston documentation: https://github./winstonjs/winston
The package you've linked to is patible with Nuxt2 and doesn't list Nuxt3 as a patible framework. In addition, there's been no mits for 3+ years. I found an issue on that package addressing this very thing with official feedback saying it would require some reworking: https://github./aaronransley/nuxt-winston-log/issues/10
I would remend either menting in that thread, or forking the project and adding patibility yourself. You may also want to contact the author about contributing with them towards a Nuxt3 version.
in this case , we can try nuxt3-winston-log
nuxt3-winston-log is a Nuxt 3.x modules to add winston-powered logging to your Nuxt application . it is very simple to use , just use console.log and console.error to log everythings .
Step 1
Install npm package
yarn add nuxt3-winston-log
Step 2
Edit your nuxt.config.js file to add module
{
modules: ["nuxt3-winston-log"];
}
Step 3
Change options using the nuxt3WinstonLog key as needed
we can see more details in nuxt3-winston-log
本文标签: javascriptAdd Winston Logging to Nuxt3Stack Overflow
版权声明:本文标题:javascript - Add Winston Logging to Nuxt3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744055088a2583109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论