Winston 记录器 - 是否可以记录应用程序的关闭

Winston logger - Is it possible to log the shut down of an application

在用于节点 js 的 Winston 记录器中,是否可以记录节点应用程序的关闭?例如,如果节点应用程序在 docker 中 运行,并且 docker 容器被杀死,是否可以使用 winston 记录?或者我需要通过 docker?

登录吗?

您可以 capture signal events in node.js and run any code on that signal,在您的情况下是日志记录。

SIGTERM - nodejs 正常关闭

A docker stop 将发送一个 SIGTERM 信号,这是发送到 *nix 进程的标准 'close all your files and connections and stop the program' 信号。您可能也想以相同的方式处理 SIGINT(ctrl-c 发送这个)。

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

SIGKILL - 不是那么优雅的 kill

A docker killdocker stop 超时将发送 SIGKILL。您无法从 node.js 捕获 SIGKILL,因为这是内核 "kill it and don't ask questions" 摆脱进程的最后手段。要捕获一些用于杀戮的日志,您需要查看 dockers 日志,或其他一些外部 monitoring/logging,因为杀戮可能来自任何地方(例如 kill -9 命令或内存不足内核中的管理器)。

合并应用程序和 Docker 日志

您可以使用 docker logging driver that matches your Winston transport if they are both logging to a central location. Syslog, Graylog(gelf) and Fluentd 将您的 docker 日志与您的应用程序日志结合起来。docker logging driver that matches your Winston transport if they are both logging to a central location. Syslog, Graylog(gelf) and Fluentd 是开源日志收集器,可以从两者中使用。