question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transports.File is not flushing on end.

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v10.10.0
  • Operating System? (Windows, macOS, or Linux)
  • Language? (all | TypeScript X.X | ES6/7 | ES5 | Dart)

What is the problem?

File transport is not flushing on exit. Even when flushing per https://github.com/winstonjs/winston#awaiting-logs-to-be-written-in-winston

Steps to reproduce:

'use strict';

let winston = require('winston');
// VERSION: "winston": "^3.1.0",

let file_transport = new winston.transports.File({
    filename: `${__dirname}/myfile.log.json`,
});
let logger = winston.createLogger({
    transports: [file_transport],
});


async function run() {
    for (let i = 0; i < 1000; i++) {
        logger.info('message');
    }

    logger.info('THE LAST MESSAGE');

    await new Promise(resolve => {
        file_transport.on('finish', resolve);
        logger.end();
    });

    process.exit(1);
}

run();

What do you expect to happen instead?

all messages (including THE LAST MESSAGE) should be written to the file.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:11

github_iconTop GitHub Comments

3reactions
ronanquilleverecommented, Aug 16, 2019

I am experiencing the exact same issue, the posted code by @racquetmaster is correct. It makes it almost impossible to use winston with asynchronous processing

I have been following the documentation (below) calling the end() on Logger. I also tried directly on (File) Transport like @racquetmaster without success. IMO this is a very important issue.

Often it is useful to wait for your logs to be written before exiting the process. Each instance of winston.Logger is also a [Node.js stream]. A finish event will be raised when all logs have flushed to all transports after the stream has been ended.

'use strict'

const winston = require('winston')

const logFilePath = __dirname + '/test-async.log'

const logger = winston.createLogger({
  level: 'debug',
  format: winston.format.combine(
    winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    winston.format.simple(),
    winston.format.printf(info => {
      return `[${info.timestamp}] [${info.level}] ${info.message}`
    })),
  transports: [
    new winston.transports.File({
      filename: logFilePath
    })
  ]
})

let code = 1
const asyncFunction = async function(){
  return new Promise(function(resolve) {
    setTimeout(function() {
      logger.info('resolved')
      resolve(0)
    }, 300)
  })
}

const start = async function(){

  logger.info('START')

  code = await asyncFunction()
  logger.info(`code : ${code}`)
  logger.on('finish', function() {
    process.exit(code)
  })

  //Should display END before exiting the process according to doc
  logger.info('END')
  logger.end()
}

start()
1reaction
yogeshbizcommented, Oct 6, 2021

This is still a major problem searched on internet. Graceful exit is okay to advise but most cases, developers just call exit(), which does not executes all pending async task. But this “caution” advisory must be shouted loudly in Winston documents. Program must wait to drain everything, do you think it is obvious to guess this, even knowing node is async? Make it easy for developers to create robust and reliable logging. Could there be a sync call that returns when all transports are written and flushed.

Found this, https://www.npmjs.com/package/winston-log-and-exit after wasting 6-7 hours.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to flush winston logs? - node.js - Stack Overflow
There will be nothing that is not flushed. The default winston FileTransport only buffers logs when attempting to open the underlying the file ......
Read more >
4.5.4 mysqldump — A Database Backup Program
The mysqldump client utility performs logical backups, producing a set of SQL statements that can be executed to reproduce the original database object ......
Read more >
EFFECT OF DRIPLINE FLUSHING ON SUBSURFACE DRIP ...
ABSTRACT. The velocity of dripline flushing in subsurface drip irrigation (SDI) systems affects system design, cost, management, performance, and longevity.
Read more >
Winston - Best of JS
file ] }); logger.info('Will not be logged in either transport!'); transports.console.level = 'info' ...
Read more >
Zlib Flush Modes
In this document, we consider only DEFLATE and not those outer formats. ... moments (e.g. at the end of a record, for transport...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found