Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
How to write log to console and file on Node.JS?

I want how to log both to file and console. This is my code snippet:

I want to make it possible to both record to console and record to file. The code below works, but is there any way to do what I just mentioned?

var fs = require("fs");
var Logger = (exports.Logger = {});
var infoStream = fs.createWriteStream("logs/info.txt");
var errorStream = fs.createWriteStream("logs/error.txt");
var debugStream = fs.createWriteStream("logs/debug.txt");

Logger.info = function(msg) {
  var message = new Date().toISOString() + " : " + msg + "\n";
  infoStream.write(message);
};

Logger.debug = function(msg) {
  var message = new Date().toISOString() + " : " + msg + "\n";
  debugStream.write(message);
};

Logger.error = function(msg) {
  var message = new Date().toISOString() + " : " + msg + "\n";
  errorStream.write(message);
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would use winston.

You can create different transports with different options.

export const Logger = winston.createLogger({
  transports: [
    new winston.transports.Console({ silent: process.env.NODE_ENV == "test" }),
    new winston.transports.File({ filename: "log/error.log" }),
  ],
})

For instance if you create a transport for debug/info/error and combine it with silent option so only the wanted files go to the log, there is also a level configuration key, but it will filter out the errors below that level.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!