Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda