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

153
Views
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 answers
Answer question

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