Winston logger - Is it possible to log the shut do

2019-07-30 22:09发布

In Winston logger for node js, is it possible to log the shut down of a node application? For example, if a node app is run in a docker, and the docker container is killed, is it possible to log that with winston? Or do I need to log it through docker?

1条回答
Lonely孤独者°
2楼-- · 2019-07-30 22:45

You can capture signal events in node.js and run any code on that signal, in your case the logging.

SIGTERM - a graceful shutdown in nodejs

A docker stop will send a SIGTERM signal, and that's the standard 'close all your files and connections and stop the program' signal that is sent to a *nix process. You probably want to handle a SIGINT in the same way too (a ctrl-c sends this).

process.on('SIGTERM', function() {  
  winston.log('Got a SIGTERM, exiting');
  process.exit(1);
});

SIGKILL - not so graceful kill

A docker kill or a timeout on docker stop will send a SIGKILL. You can't capture a SIGKILL from node.js as this is the kernels "kill it and don't ask questions" last resort to get rid of a process. To capture some logs for a kill you would need look at dockers logs, or some other external monitoring/logging as kills can come from anywhere (For example a kill -9 command or the Out Of Memory manager in the kernel).

Combining application and Docker logs

You can combine your docker logs with your application logs by using a docker logging driver that matches your Winston transport if they are both logging to a central location. Syslog, Graylog(gelf) and Fluentd are open source log collectors and can be used from both.

查看更多
登录 后发表回答