Node js - Console Logger - node js examples - node js tutorial

  • Let’s discuss on the basic nodejs tutorial on custom logging the information or error in to the file.
  • Foe each and every scenario there are two routes success or failure. If the process succeeds write it in output file but if there is an error write it in error Output file as shown below:
 nodejs console logger

Learn Nodejs - Nodejs tutorial - nodejs console logger - Nodejs examples - Nodejs programs

  • This scheme is achieved using filestream in node.js.

console Logger code :

const output = fs.createWriteStream  ('D:\\wikiTechy\\7.node.js\\4.node-js-console\\stdout.log');
const errorOutput = fs.createWriteStream('D:\\wikiTechy\\7.node.js\\4.node-js-console\\stderr.log');
const Console = require('console').Console;
const logger = new Console(output, errorOutput); 	
var count = 5;

Code Explanation :

 nodejs console logger code

Learn Nodejs - Nodejs tutorial - nodejs console logger code - Nodejs examples - Nodejs programs

  1. Create write file stream in the path D:\wikiTechy\7.node.js\4.node-js-console\stderr.log and it is assigned to the variable output.
  2. Create write file stream in the path D:\wikiTechy\7.node.js\4.node-js-server\stderr.log for logging errors and it is assigned to the variable errorOutput.
  3. Create Console global variable and made it as required field.
  4. Create logger object relative to Console class variable with the parameters as both output and errorOutput.
  5. Declare and initialize the count variable and now we are trying to log using the line
  • logger.log('count: %d', count);
  • and this is similar to
  • console.log('count: %d', count);
  • Properties of console is inherited with the name logger and am trying to use it.
 nodejs console logger node

Learn Nodejs - Nodejs tutorial - nodejs console logger node - Nodejs examples - Nodejs programs

Entering into the nodejs framework to execute repl commands

  1. Create the 1st file to store the output
  2. Create the 2nd file to store the error output
  3. Make the Console variable as mandatory.
  4. Create logger object and inherit the Console option with 1st and 2nd file as parameters that is output and errorOutput.
  5. Initialize the variable count which is 5 here.
  6. logger.log will display the output as count: 5 in stdout as shown below:
 console logger output

Learn Nodejs - Nodejs tutorial - console logger output - Nodejs examples - Nodejs programs

Related Searches to node.js - Console Logger

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add to your ad blocking whitelist or disable your adblocking software.