我有一个Splunk转发器管理我的生产服务器中的日志,所以我真的只需要将我的节点应用程序的输出放到Splunk正在观看的文件中。在生产中简单地执行以下操作的缺点是什么:
node server.js &> output.log
反对使用某种日志记录模块处理节点进程内的日志输出...
答案 0 :(得分:2)
这是一个典型的配置文件:/etc/supervisor/conf.d/supervisord.conf
[supervisord]
nodaemon=true
logfile=GKE_MASTER_LOGDIR/supervisord_nodejs_GKE_FLAVOR_USER.log
pidfile=GKE_MASTER_LOGDIR/supervisord_nodejs_GKE_FLAVOR_USER.pid
stdout_logfile_maxbytes = 1MB
stderr_logfile_maxbytes = 1MB
logfile_backups = 50
# loglevel = debug
[program:nodejs]
command=/tmp/boot_nodejs.sh %(ENV_MONGO_SERVICE_HOST)s %(ENV_MONGO_SERVICE_PORT)s
stdout_logfile = GKE_MASTER_LOGDIR/nodejs_GKE_FLAVOR_USER_stdout.log
stderr_logfile = GKE_MASTER_LOGDIR/nodejs_GKE_FLAVOR_USER_stderr.log
stdout_logfile_maxbytes = 1MB
stderr_logfile_maxbytes = 1MB
logfile_backups = 50
autostart = True
autorestart = True
# user = GKE_NON_ROOT_USER
在我的情况下,这一切都发生在Docker容器中,所以这里是我的Dockerfile的片段,它启动supervisord,然后启动nodejs,这样做将stdout / err重定向到日志文件,supervisord根据空间和/或时间旋转...使用Docker与使用supervisord正交,因此YMMV
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf" ]
为了完整性,我包括上面引用的boot_nodejs.sh
#!/bin/bash
given_mongo_service_host=$1
given_mongo_service_port=$2
current_dir=$(dirname "${BASH_SOURCE}")
current_timestamp="timestamp "$(date '+%Y%m%d_%H%M%S_%Z')
echo
echo "______________ fresh nodejs server bounce ______________ $current_timestamp"
echo
# ............... now output same to standard error so its log gets the hat tip
(>&2 echo )
(>&2 echo "______________ fresh nodejs server bounce ______________ $current_timestamp" )
(>&2 echo )
# ................
export MONGO_URL=mongodb://$given_mongo_service_host:$given_mongo_service_port
type node
node main.js
答案 1 :(得分:1)
将输出重定向到日志文件没有问题。在很多方面,这是更可取的。
当您的应用程序很复杂并且需要大量日志配置(可能写入多个日志文件)时,让应用程序直接写入日志会更有用。我所做的是使用Winston进行日志记录。通常,启用的唯一日志传输是控制台,如果需要,我可以将其重定向到文件。但是,我也在我的应用程序配置中指定了其他传输和配置。我用它直接写入Logstash等。