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

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

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 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