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 combination of tools working.
I tried pulling in the Nuxt Winston package anyway (https://www.npmjs.com/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.
2
Answers
So 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
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 composables. Filename:
composables/logging.ts
Step 4 (If you did Step 3)
Use the logger. Filename:
plugins/50.myplugin.js
Notes
nuxtApp.$logger.log
. After all this rigamarole, I just put// @ts-ignore
and moved on.provide
the logger itself from the plugin, not the logger.log functionThe package you’ve linked to is compatible with Nuxt2 and doesn’t list Nuxt3 as a compatible framework. In addition, there’s been no commits 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.com/aaronransley/nuxt-winston-log/issues/10
I would recommend either commenting in that thread, or forking the project and adding compatibility yourself. You may also want to contact the author about contributing with them towards a Nuxt3 version.