# How to check if the logfile of a Logger instance is closed?

**URL:** <https://www.ruby-forum.com/t/how-to-check-if-the-logfile-of-a-logger-instance-is-closed/92258>\
**Category:** Ruby\
**Created:** [April 17, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/how-to-check-if-the-logfile-of-a-logger-instance-is-closed/92258 "2007-04-17T16:26:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [April 17, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/how-to-check-if-the-logfile-of-a-logger-instance-is-closed/92258/1 "2007-04-17T16:26:33Z")

</div>

If I do:

l = Logger.new(“testlog”)  
l.close

now if I want to close it again, I get:  
IOError: closed stream

So far, I have checked for this error (by rescue clause), to see, if  
the logfile is already closed. Is there a more elegant way to check if  
the instance (i.e. the logfile stream) is already closed?

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [April 17, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/how-to-check-if-the-logfile-of-a-logger-instance-is-closed/92258/2 "2007-04-17T16:49:36Z")

</div>

On Tue, 17 Apr 2007, ChrisKaelin wrote:

> the instance (i.e. the logfile stream) is already closed?  
> Logger doesn’t provide a method to check this, so what you are doing is  
> right.

You can look at log4r ([http://log4r.sourceforge.net/](http://log4r.sourceforge.net/)) for a logger that  
supports a closed? method, and which is pretty flexible.

You might also look at my asynchronous logger, Analogger  
([http://analogger.swiftcore.org](http://analogger.swiftcore.org)). It supports a closed? method, as  
well.

It also looks like it would be pretty easy to subclass Logger and  
LogDevice to create a version that implements closed?.

Kirk H.

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [April 17, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/how-to-check-if-the-logfile-of-a-logger-instance-is-closed/92258/3 "2007-04-17T17:25:41Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > So far, I have checked for this error (by rescue clause), to see, if  
> > ([http://analogger.swiftcore.org](http://analogger.swiftcore.org)). It supports a closed? method, as  
> > well.
> 
> It also looks like it would be pretty easy to subclass Logger and  
> LogDevice to create a version that implements closed?.
> 
> Kirk H.  
> Thanks, I was thinking too of subclassing Logger for that, but the  
> rescue clause seems to do this work well enough 😉

Analogger looks great, but for my simple script framework it is a bit  
too much. I’ll check it, if I do bigger projects in the future…
