Raised exceptions are not logged when USR2

Eric Wong normalperson at yhbt.net
Mon Jan 4 17:34:06 EST 2010

Iñaki Baz Castillo <ibc at aliax.net> wrote:
> El Domingo, 3 de Enero de 2010, Eric Wong escribió:
> >   # Totally untested, stick this in your Unicorn config file and let
> >   # us know if it works or blows up badly:
> > 
> >   require 'syslog_logger'
> > 
> >   class MySyslogLogger < SyslogLogger
> >     alias puts error
> >     alias write error
> >     def flush; self; end
> >   end
> > 
> >   $stderr = MySyslogLogger.new('foo')
> Unfortunatelly it doesn'w work very well. First of all Sysloglogger relies on 
> a constant called SYSLOG created in "initialize" method so it's required to 
> add the above methods to SyslogLogger class rather than subclass.
> Anyhow, the main problem is that by doing "$stderr = SyslogLogger.new('foo')" 
> the std error is not redirected as /proc/PID/fd still shows:
>   2 -> /dev/pts/2

Yup, that's expected.  _Assigning_ $stderr does not do a redirect at the
OS-level, only at th Ruby level (sufficient for env["rack.errors"],

> Of course by calling $stderr.puts "error" the above works, but when a real 
> error raised it's is printed in the screen (/dev/pts/2).

> I strongly think that IO#reopen is required. IO#reopen accepts a string (file 
> path) or other IO, so "converting" SyslogLogger into a IO is required.
> I'll try to achieve it.

Yes, you need to do something that'll dup()/dup2() internally like
IO#reopen to replace fd=2.  But when you have the raw file descriptor,
you won't get syslog formatting...

The only way I can think of is to redirect fd=2 to the stdin of a
separate process to add formatting in...

  r, w = IO.pipe
  fork {
     exec('ruby', '-rsyslog', '-ane',
          'BEGIN{Syslog.open("foo")}; Syslog.warning $_')

Not pretty...

Eric Wong

More information about the mongrel-unicorn mailing list