问题描述
在用于节点js的Winston logger中,是否可以记录关闭节点应用程序?例如,如果节点应用程序在Docker中运行,并且docker容器被杀死,是否可以使用winston进行登录?或者我需要通过码头登录吗?
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?
推荐答案
您可以,并运行,在您的情况下记录。
You can capture signal events in node.js and run any code on that signal, in your case the logging.
A docker stop
将发送一个 SIGTERM
信号,这是标准'关闭所有文件和连接,并停止发送到* nix进程的程序'信号。你可能想要以同样的方式处理一个 SIGINT
(一个ctrl-c发送这个)。
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 - 不那么优雅杀死
docker kill
或超过 docker stop
将发送一个 SIGKILL
。您不能从node.js捕获 SIGKILL
,因为这是内核杀死它并且不提问题最后的手段去除一个进程。要捕获一些杀死的日志,您将需要查看docker日志,或者其他外部监视/日志记录可能会从任何位置(例如 kill -9
命令或内核中的内存管理器)。
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).
您可以组合您的docker使用符合您的码头记录驱动程序与您的应用程序日志记录如果他们都记录到中央位置,则href =https://github.com/winstonjs/winston/blob/master/docs/transports.md =nofollow> Winston transport 。 ,(gelf)和是开源日志收集器,可以从两者中使用。
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.
这篇关于Winston logger - 可以记录应用程序的关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!