This article was originally published here.
In this tutorial, we will discuss how to correctly perform logging on NodeJS. We will start from simpl...
For further actions, you may consider blocking this person and/or reporting abuse
if you used pino you wont need asynclocalstorage, pino-http already has this built in
Could you give an example of how that is possible in a project with
express
? The only way I see usingpino-http
is to do something like this:req.log.info('something')
, but you need to have access to the request object. If you want to use the main logger and show the request id, how would you do?Thanks!
Great writeup! Do you use Sentry to store all your logs? That's what I do for front-end logs as well. But, back-end logs I store in Sematext.
Thanks Adnan,
I usually store only backend logs on Sentry. I hadn't heard of Sematext, but it looks cool. In my opinion, Elasticsearch is a very good tool to store logs and has great searching capabilities. Based on your experience, how many frontend logs can you store on the free 500MB/day plan?
Yeah, Elasticsearch is awesome! My team-mates are managing a huuuge cluster in our prod. I have nothing but immense respect for those guys. You have to be borderline genius to run that efficiently. 😄
Aha, so you're doing the opposite than me. I've been running Sematext for logs and monitoring across my whole back end and infra for my side-gig/startup for the last year. I've never crossed 500MB/day for logs. And mind you, I had both Node.js and Nginx logs set up. But, even if you do go over 500MB/day, it's not like you'll get charged crazy amounts like with CloudWatch or Datadog. I've heard crazy stories. The pricing model with Sematext is flexible and hard to understand at times. The gist of it is that the price increases at a fixed rate with retention while increasing the ingestion is super lenient because there's a reserved volume, not a limit. If you cross the reserved volume, you get charged peanuts. E.g. on the $50/month plan you get 30GB/month, and if you use 35GB/month, you'll get charged $58.35/month. There are no penalties whatsoever. If you don't want to go over 30GB/month you can define you own cap, and tell it to stop ingesting altogether. So yeah, flexible but sometimes hard to understand. 😄
I'm currently maintaining open-source repos at Sematext (if you see any Node.js-related things, that's me 😄) and building integrations. I can't give you more info about product-related things, but I can point you in the right direction if you want to know more. 😄
Sounds very interesting. I will definitely give it a try.
Just a comment about something that I forgot to mention in the article. The part with the local storage UUID doesn't work in earlier versions of NodeJS. It didn't even work on earlier versions of NodeJS 13, so I used v13.12 in this tutorial.
Super useful and very very well explained. Thanks for sharing!
Thanks, Antonio
I am really glad that you liked it!!!
bunyan has a different but equally useful approach to logging. I highly recommend it especially if you push your logs to CloudWatch and want to use CloudWatch Insights.
What I like really about bunyan, although I usually use winston, is that it separates the logic of saving logs from the logic of formating logs. So you can have raw JSON logs that can be streamed anywhere, but at the same time, you can just pipe the logs to the bunyan CLI and get a nice, colored, human-readable output.
Nice!!
Thank you Jaime!!!