Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging stdout and stderr of Node

I am using the boilerplate code of mean.io and starting my server with the command:

node server.js 

How do I log stdout and stderr of my Express application?

Here's my file server.js:

'use strict';  /**  * Module dependencies.  */ var mongoose = require('mongoose'),     passport = require('passport'),     logger = require('mean-logger');  /**  * Main application entry file.  * Please note that the order of loading is important.  */  // Initializing system variables var config = require('./server/config/config'); var db = mongoose.connect(config.db);  // Bootstrap Models, Dependencies, Routes and the app as an express app var app = require('./server/config/system/bootstrap')(passport, db);  // Start the app by listening on <port>, optional hostname app.listen(config.port, config.hostname); console.log('Mean app started on port ' + config.port + ' (' + process.env.NODE_ENV + ')');  // Initializing logger logger.init(app, passport, mongoose);  // Expose app exports = module.exports = app; 
like image 561
raju Avatar asked May 26 '14 02:05

raju


People also ask

What is stdout and stderr in node JS?

stdout (1): The standard output stream, which is a source of output from the program. process. stderr (2): The standard error stream, which is used for error messages and diagnostics issued by the program.

Should logs go to stdout or stderr?

Non-error logs should go to stdout, not stderr #60.

What is logging in Nodejs?

Node. js logging is an important part of supporting the complete application life cycle. From creation to debugging to planning new features, logs support us all the way. By analyzing the data in the logs, we can glean insights, resolve bugs much quicker, and detect problems early and as they happen.

Does console log go to stdout?

log is great for printing messages in the Console. This is what's called the standard output, or stdout . console. error prints to the stderr stream.


2 Answers

What about this?

console.log("I will goto the STDOUT"); console.error("I will goto the STDERR"); 

Note: both of these functions automatically add new line to your input.

If you don't want those newlines appended to your input, do this

process.stdout.write("I will goto the STDOUT") process.stderr.write("I will goto the STDERR") 

Both process.stdout and process.stderr are streams, so you can even pipe a stream into them. See Node.js docs on streams for more info.

like image 119
Steel Brain Avatar answered Oct 13 '22 23:10

Steel Brain


You can do this by writing to stdout and stderr streams

process.stdout.write('Hello') 

or

process.stderr.write('Error') 

Better will be to use some thirdparty logging module like winston or bunyan

like image 22
ashu Avatar answered Oct 13 '22 23:10

ashu