Hi, I am working on creating a daemon in Ruby using the daemons gem. I want to add output from the daemon into a log file. So I am wondering what is the easiest way to redirect puts
from the console to a log file?
views:
1665answers:
3
+5
A:
Try
$stdout = File.new( '/tmp/output', 'w' )
To restore:
$stdout = STDOUT
Pistos
2008-10-22 04:55:36
+6
A:
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.
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:
#!/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"
VitalieL
2008-10-22 06:26:33