Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda