Search code examples
rubyloggingdaemons

Ruby: Logger and Daemons


i'm using ruby 1.9.2p180 (2011-02-18 revision 30909)

in Order to do the logging i use the logging gem. My program has two blocks, which are used as daemons.

But logging from these blocks results in an error and nothing is written to the logfile:

log shifting failed. closed stream
log writing failed. closed stream

Here is what happens in the code:

log = Logger.new(logbase + 'logfile.log', 'monthly')
log.level = Logger::INFO

proc = Daemons.call(options) do
  # [...]
  log.info "Any Logmessage"
  # [...]
end

Any Idea, whats wrong there?


Solution

  • The Daemons gem closes all file descriptors when it daemonizes the process. So any logfiles that were opened before the Daemons block will be closed inside the forked process.

    And since you can't write to closed file descriptors -> errors.

    You can read more about what happens when you daemonize a process by reading the chapter:

    What does daemons internally do with my daemons?
    http://daemons.rubyforge.org/Daemons.html

    The solution is to open the logfile inside the daemon block instead of outside of it. That should fix it. But note that daemonizing changes the working directory to /, so take that into account when referencing logfile paths.