Logging NodeJS application in production - node.js

I am a beginner to Node. Would anyone tell how to handle errors in NodeJS? Also how to define node server to write its logs to a default file instead writing to console.

Winston is a good npm package for logging data to files or to a database (Mongo).

Related

Third party redis client logging directly to stdout in node - would like it to be JSON

I have a node service which logs to stdout as JSON. However, the problem is that certain npm packages I'm using will within their own functions log directly to stdout. One of the examples is ioredis, which will not throw an error if the client fails to initialize. Instead, it will just write to stdout so there's no hook for me to catch and log in the right format. Other libraries are like this as well.
I've considered wrapping/monkey-patching the native node console.log, but that seems like a bad idea because most logging libraries ultimately rely on console and I'd be changing the native behavior.
Is there a widely-used pattern for handling this?

Winston for logging a mulitlpe container application

I am planning to use the Digital Ocean App Platform to host my backend but I wanted to know if each container in App platform would have a different log file (assuming I’m logging into files with Winston) and if this is the case would this even be an issue.
I thought of multiple solutions in case I should handle this:
1- Log into the database
2- Make another container that expects to get the logs through HTTP from the other running containers.
(Note I'm new with dealing with containers so I might be missing/misunderstanding something)
Yes, you will get log files from each node.js process. For this scenario Winston supports alternative transports that can centralize logs from many sources.
As you suggested (2) some of these transport options write logs to an RDBMS.

What is sense of NodeJS modules without any exports?

If there's any. Im not really into web technologies, but have to understand some awful code written by someone else in Node.
All node.js apps are npm modules once you execute npm init. After that point, you can publish the module by executing npm publish assuming you gave it a unique name in the package.json.
If the app isn't meant to return anything, then there is no need to export anything. However, it's almost always worth exporting something to allow for unit testing deeper than just starting the app as an http server and sending it requests.
It is also sometimes useful to modify the way your app runs based on whether it is being required as a module or executed as an app. For example, say i have an express rest api server. I can run it as a standalone server on api.example.com, and then require it into another application and run it from that application directly to avoid CORS issues without having to duplicate code or deal with git submodules, instead I simply npm install the api into the application that needs it and attach it just like you would a router. www.example.com/api
app.use('/api', require('#myusername/api.example.com'))

Can I log socket.io debug information to a file?

I'm running node.js with socket.io and I'd like to log the debug information (the information that shows in the node.js console when you run socket.io with default options) to a file, is there any way to do this?

Socket.io - Easiest way to log values from the server

When you are testing your javascript on the server (server.js) file using socket.io, you can't use console.log(), or alert().
What is the easiest way to log or alert a value for testing purposes?
Of course you can use console.log
It just gets logged to the terminal.
Node Docs on console
I log to syslog via the node-syslog package that way I can still see the logs even when the server is running in the background.

Resources