# Why does "warn" print a extra \\n?

**URL:** <https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064>\
**Category:** Ruby\
**Created:** [January 4, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064 "2010-01-04T21:40:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 4, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064/1 "2010-01-04T21:40:09Z")

</div>

Hi, by redirecting $stderr to a logger (Logger or SyslogLogger instance)  
I’ve  
realized that Kernel#warn prints an extra \n so the logger is called  
twice  
(the second one with an empty line).

The “warn” code shows it clearly:

static VALUE  
rb\_warn\_m(self, mesg)  
VALUE self, mesg;  
{  
if (!NIL\_P(ruby\_verbose)) {  
rb\_io\_write(rb\_stderr, mesg);  
rb\_io\_write(rb\_stderr, rb\_default\_rs);  
}  
return Qnil;  
}

So for example by redirecting $stderr to a Logger instance

warn “hello”

I get:

# Logfile created on 2010-01-04 21:32:21 +0100 by logger.rb/20321

F, [2010-01-04T21:32:38.981099 #22370] FATAL – : hello  
F, [2010-01-04T21:32:38.981385 #22370] FATAL – :

Can this be avoided (without rewritting “warn” call)? Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 5, 2010, 1:56pm UTC](https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064/2 "2010-01-05T13:56:32Z")

</div>

On 01/04/2010 09:39 PM, Iñaki Baz C. wrote:

> ```
> if (!NIL_P(ruby_verbose)) {
> 
> ```
> 
> I get:
> 
> # Logfile created on 2010-01-04 21:32:21 +0100 by logger.rb/20321
> 
> F, [2010-01-04T21:32:38.981099 #22370] FATAL – : hello  
> F, [2010-01-04T21:32:38.981385 #22370] FATAL – :
> 
> Can this be avoided (without rewritting “warn” call)? Thanks.

Frankly, I believe redirecting stderr to a logger instance is a broken  
approach. Why? A logger has a completely different interface, i.e. it  
accepts at least a log level and the message to log. Based on that it  
decides whether to output the message and how to format it. A stream  
like stderr readily accepts anything send to it and it will output it  
(if there are no errors of course). There is no formatting or deciding  
going on.

I do not know your use case or what you want to achieve with this but it  
seems redirecting a stream of bytes to a logger is not a good idea on  
the design level already.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 5, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064/3 "2010-01-05T14:19:21Z")

</div>

El Martes, 5 de Enero de 2010, Robert K.  
escribió:\> the design level already.  
Yes, you are right. I did a hack by creating a modification of  
SyslogLogger  
and then:

$stderr = SyslogLogger.new(“foo”)

However it’s really a hack since it’s just valid for “warn” method and  
some  
others using “$stderr.puts”. But the real stderr remains being  
/dev/pts/N so  
if the application raises it would write on the terminal.

Finally I’ve dropped the idea of using a Logger as $stderr.

Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 5, 2010, 2:30pm UTC](https://www.ruby-forum.com/t/why-does-warn-print-a-extra-n/181064/4 "2010-01-05T14:30:29Z")

</div>

On the other hand, that \n may come in handy in line-buffered I/O, and  
leaves a clean start to the next and probably unrelated write.
