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

354
Visualizações
How to avoid error level message from info file using winston log file?

I am a beginner in Node JS. I am working on winston log file. I am facing an issue with Winston logger (Node JS). When I run the command "npm start" it shows some error level logs in both the error log file and info log file at an initial run. For all other cases, it works fine.

Also, the warning file shows both error and info level messages. In debug file, there also shows both the error and debug messages all the time. What can be the reason for it? How to log messages in the right manner such a way that error messages logs in the error file, info messages in the info file, warning messges in warn file and debug message in debug file. Please refer to the below code.

Code

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

const { combine, timestamp, prettyPrint, printf } = format;

const customFormat = printf(({ level, message, timestamp }) => {
    if (!message.includes('&')) {       
        return `${timestamp} ${level}: ${message}`;
    } else {
        return `${timestamp} - ${message.split('&')[0]} - ${level}: ${message.split('&')[1]}`;
    }
});

const logger = createLogger({
    format: combine(
        timestamp(),
        // prettyPrint(),
        customFormat
    ),
    transports: [       
        new transports.File({
            filename: `${__dirname}/log/error.log`,
            level: 'error'
        }),
        new transports.File({
            filename: `${__dirname}/log/info.log`,
            level: 'info'
        }),
        new transports.File({
            filename: `${__dirname}/log/warn.log`,
            level: 'warn'
        }),
        new transports.File({
            filename: `${__dirname}/log/debug.log`,
            level: 'debug'
        }),
        new transports.Console({
            timestamp: true
        }),
    ],
    exceptionHandlers: [
        new transports.Console({
            json: false,
            timestamp: true
        }),
        new transports.File({
            filename: `${__dirname}/log/exceptions.log`,
            json: false
        })
    ],
    exitOnError: false
});

module.exports = logger;


about 4 years ago · Juan Pablo Isaza
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