当在服务器上部署Shiny应用程序时,如何将标准输出写入日志文件?

时间:2015-11-25 08:59:05

标签: logging server shiny

我无法获取闪亮应用的日志信息。

在R命令提示符下通过runApp()在本地计算机上运行时 我通过cat(),print(),str()等进行了大量打印。 我希望在服务器上部署时在日志文件中看到这一点。

我们有一个相当默认的闪亮服务器配置

# logs
cd /var/log
l | grep shiny
drwxr-xr-x. 2 shiny  shiny     4096 Nov 20 14:50 shiny-server
-rw-r--r--. 1 shiny  root    181558 Nov 24 16:58 shiny-server.log

cd /var/log/shiny-server
ls -ltr
...
-rw-r-----. 1 shiny shiny  1149 Nov 13 15:19 current-shiny-20151113-151944-49237.log
-rw-r-----. 1 shiny shiny  1031 Nov 20 14:50 current-shiny-20151120-145042-35634.log
-rw-r--r--. 1 shiny root  43690 Nov 24 16:58 access.log

应用程序日志文件的示例是

cat current-shiny-20151125-154719-44403.log

Listening on http://127.0.0.1:44403
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.3/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)

这不是我写给标准的东西,我想以某种方式看到。

我相信如果成功退出进程,也会清除日志文件。我能阻止这个吗?

申请流程在哪里? 看来应用程序是通过SockJSAdapter.R进程运行的(见下文)。 即使从多个浏览器窗口(例如在Firefox和Chrome中)同时访问URL,每个访问的URL也只有一个。 在下面3个不同的URL可以从4个浏览器窗口访问。

ps -alef | egrep "PID|shiny" | grep -v grep
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root     24734     1  0  80   0 - 50506 wait   15:15 ?        00:00:00 /usr/bin/su shiny -c /opt/shiny-server/bin/shiny-server --pidfile=/var/run/shiny-server.pid >> /var/log/shiny-server.log 2>&1
4 S shiny    24736 24734  0  80   0 - 28279 wait   15:15 ?        00:00:00 bash -c /opt/shiny-server/bin/shiny-server --pidfile=/var/run/shiny-server.pid >> /var/log/shiny-server.log 2>&1
0 S shiny    24737 24736  0  80   0 - 245601 ep_pol 15:15 ?       00:00:02 /opt/shiny-server/ext/node/bin/shiny-server /opt/shiny-server/lib/main.js --pidfile=/var/run/shiny-server.pid
0 S shiny    24753 24737  0  80   0 - 91424 ep_pol 15:15 ?        00:00:07 /usr/lib64/R/bin/exec/R --no-save --slave -f /opt/shiny-server/R/SockJSAdapter.R
0 S shiny    25091 24737  1  80   0 - 121894 ep_pol 15:32 ?       00:00:07 /usr/lib64/R/bin/exec/R --no-save --slave -f /opt/shiny-server/R/SockJSAdapter.R
0 S shiny    25188 24737  1  80   0 - 91335 ep_pol 15:39 ?        00:00:03 /usr/lib64/R/bin/exec/R --no-save --slave -f /opt/shiny-server/R/SockJSAdapter.R

2 个答案:

答案 0 :(得分:6)

我遇到了同样的问题并解决了如下问题:

默认情况下,部署在服务器上的闪亮应用程序将在/var/log/shiny-server/中包含一个日志记录文件。出于某种原因,只记录了stderr - see this discussion。为了将stdout重定向到这个文件,我在Server.R文件的开头有一个语句:

if (!interactive()) sink(stderr(), type = "output")

这意味着本地我不重定向,因为会话是交互式的。所以基本上我将stdout重定向到stderr并不会让我开心,但只要我不想管理我自己的日志文件就是我的工作。现在记录了当你在本地运行闪亮应用程序时通常可以在R-REPL中看到的所有内容。

答案 1 :(得分:0)

您可以使用sinkcapture.output来捕获标准输出的输出。