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

158
Vistas
Winston is only logging .info errors and doesn't log error to file or mongodb

I am using express-mongoose for my backend and winston as my logger. The problem is that winston is only logging the info messages not errors The logs in server.log enter image description here

Error messages only get logged on console but neither in the file nor mongodb enter image description here

the logger code

const { createLogger, format, transports } = require('winston');

// Import mongodb
require('winston-mongodb');

module.exports = createLogger({

transports:[
//Console transport
    new transports.Console(),

// File transport
    new transports.File({
    filename: 'logs/server.log',
    format:format.combine(
        format.timestamp({format: 'MMM-DD-YYYY HH:mm:ss'}),
        format.align(),
        format.printf(info => `${info.level}: ${[info.timestamp]}: ${info.message}`),
    )}),

// MongoDB transport
    new transports.MongoDB({
        level: 'error',
        //mongo database connection link
        db : 'mongodb://localhost/logs',
        options: {
            useUnifiedTopology: true
        },
        // A collection to save json formatted logs
        collection: 'edushare_logs',
        format: format.combine(
        format.timestamp(),
        // Convert logs to a json format
        format.json())
    })]
});

I created an error by not defining the jwt. the code

const logger = require("./utils/logger")
if (!config.get("jwtPrivateKey")){
    logger.error("FATAL ERROR!! JWT is not defined!")
    process.exit(1)
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The winston "console transport" is synchronous, however the processes to write to mongo or file are asynchronous. In this case you can use the winston function callback. Try this:

const logger = require("./utils/logger")
if (!config.get("jwtPrivateKey")) {
    logger.error("FATAL ERROR!! JWT is not defined!", () => {
        exit(1);
    });
}
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