Search code examples
rubysyslog

Ruby - Ensure Syslog Gets Closed


Is it absolutely critical that I always close Syslog when I'm done using it? Is there a huge negative impact from not doing so?

If it turns out that I definitely need to, what's a good way to do it? I'm opening Syslog in my class constructor and I don't see a way to do class destructors in Ruby, and currently have something resembling this:

class Foo
  def initialize
    @@log = Syslog.open("foo")
  end
end

I don't immediately see the place where the Syslog.close call should be, but what do you recommend?


Solution

  • The open method accepts a block. Do something like this:

    class Foo
      def do_something
        Syslog.open do
          # work with the syslog here
        end
      end
    end