#What is the best way to store logs in Nestjs?
24 messages · Page 1 of 1 (latest)
What’s your actual logger? Winston, Pino? Most of them come with some kind of transport functionality, avoids managing files
I'm using none.
For console log, its default Logger from nest.
Interesting, do you have some repo of example ?
I read about Winston, but don't try anything yet.
AFAIR, Nest logger by default uses ConsoleLogger, if you need any other destinations - it's bring your own anyway.
At my workplace we use pino and send logs to Cloudwatch first, so logging to console is enough
https://github.com/iamolegga/nestjs-pino
Should be just adding destination to config (which is never that simple 😄 )
Though I haven’t done it myself
Thanks, I'll read about, and give a try.
How do you deploy the app? The best practise is to always let the app log into standard output (stdout/console) and have the infrastructure take care of routing the logs
For example, in Docker, you can specify the log driver - this can be file, loki, cloudwatch, and I'm sure there's a driver for elastic search, too
Hm.. how is it the best practice? You have a log buffer. Why would writing it to some intermediatery is better than sending it directly to destination? Especially for a UNIX, where everyting is a file.
I.e writing to drive is no diferrent than writing it to port
A methodology for building modern, scalable, maintainable software-as-a-service apps.
Why would writing it to some intermediatery is better than sending it directly to destination
Because, for example - if the application crashes, it can't send the logs anywhere and you lose the most important logs - those right before the crash. An app can always print to stdout -> you even get the crash logs from Node that way. It's not the application's concern to manage it's logs, it's the concern of the infrastructure to route them to the appropriate place.
also if you choose a different way to store your logs, you don't need to rebuild and redeploy your application
Only if it's not buffered
Well, yes. But for example, pino buffers logs in a separate process, so if the main process fails, the buffered logs are still usually printed out in an unhandled exception hook
That's interesting 🙂 I must be missing something. If app crashes and container is killed, where would stdout go?
The docker engine reads the stdout
logs from killed containers are still available
But also, you can just run the app with node main.ts >> logfile.txt
that's the simplest example of "the infrastructure manages the logs"
Of course, I have worked on many apps the send the logs to loki, cloudwatch, datadog, logcat, or whatever themselves. The configuration was baked into the app. It's not wrong to do it, people do it.
But you were asking what's the best way. And what I said is, in my opinion and experience, the best ways to manage logs. Let the application print to console and let the infrastucture route it.