[英]Redirect the “puts” command output to a log file
I am working on creating a daemon in Ruby using the daemons gem. 我正在使用daemons gem在Ruby中创建一个守护进程。 I want to add output from the daemon into a log file.
我想将守护进程的输出添加到日志文件中。 I am wondering what is the easiest way to redirect
puts
from the console to a log file. 我想知道将
puts
从控制台重定向到日志文件的最简单方法是什么。
I should recommend to use ruby logger, it is better than puts, you can have multiple log levels that you can turn on/off: debug, warn, info,error, etc. 我应该建议使用ruby logger,它比puts更好,你可以有多个日志级别,你可以打开/关闭:调试,警告,信息,错误等。
logger = Logger.new(STDOUT)
logger = Logger.new("/var/log/my-daemon.log")
I use runit package to manage ruby services, it has svlogd than will redirect daemon output to log file, here is run script for logger process: 我使用runit包来管理ruby服务,它有svlogd比重定向守护进程输出到日志文件,这里是logger进程的运行脚本:
#!/bin/sh
set -e
LOG=/var/log/my-daemon
test -d "$LOG" || mkdir -p -m2750 "$LOG" && chown nobody:adm "$LOG"
exec chpst -unobody svlogd -tt "$LOG"
Try 尝试
$stdout = File.new( '/tmp/output', 'w' )
To restore: 恢复:
$stdout = STDOUT
Or you can redefine the puts
command? 或者你可以重新定义
puts
命令? Works probably only in a single file/class 可能只在单个文件/类中工作
def puts(message)
#write message to file
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.