Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.exit() just after logging, doesn't log to the files. #1629

Open
1 of 2 tasks
lightyaer opened this issue Apr 3, 2019 · 2 comments
Open
1 of 2 tasks

process.exit() just after logging, doesn't log to the files. #1629

lightyaer opened this issue Apr 3, 2019 · 2 comments

Comments

@lightyaer
Copy link

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v11.13.0
  • Operating System? WSL
  • Language? ES6/7

What is the problem?

Here is how to reproduce the problem.

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({ filename: "src/logs/access.log", level: "info" }),
    new winston.transports.File({ filename: "src/logs/errors.log", level: "error" })
  ],
  format: winston.format.combine(
    winston.format.timestamp({
      format: "YYYY-MM-DD HH:mm:ss"
    }),
    winston.format.errors({ stack: true }),
    winston.format.splat(),
    winston.format.json()
  )
});

  logger.add(
    new winston.transports.Console({
      format: winston.format.combine(winston.format.colorize(), winston.format.simple())
    })
  );

  logger.info({
    level: "info",
    message: "Incoming Request"
  });
process.exit()

The outcome is, it logs to the console, but doesn't log to the files because it takes some time to log to files.

So the solution can be making the logger.log function async
Or
Can someone suggest a workaround?

What do you expect to happen instead?

It should log to all the three transports, console and 2 files.

@lightyaer lightyaer changed the title logger.log is async, please return a promise process.exit() just after logging, doesn't log to the files. Apr 3, 2019
@felix-hoc
Copy link

You should be able to wait for the "finish" event, and exit then. See this section in the README: (https://github.com/winstonjs/winston#awaiting-logs-to-be-written-in-winston)

However, this behavior seems to be broken atm, at least for File transports - they don't flush on end: #1504

@sorenlouv
Copy link

tldr: replace process.exit(1) with process.exitCode = 1;

After wasting a couple of hours I hope this will help someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants