# Buncha logging stuff showing up in unit tests

**URL:** <https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865>\
**Category:** Ruby\
**Created:** [November 23, 2005, 8:36pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865 "2005-11-23T20:36:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 23, 2005, 8:36pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/1 "2005-11-23T20:36:32Z")

</div>

I’m using Ruby’s standard Logger to log a bunch of debug, info, and  
errors.

But when I run the unit tests for the classes that use the Logger, all  
the logs are showing up in the test output.

Any good solutions for solving this?

---

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 23, 2005, 8:40pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/2 "2005-11-23T20:40:35Z")

</div>

On Nov 23, 2005, at 11:32 AM, Joe Van D. wrote:

> I’m using Ruby’s standard Logger to log a bunch of debug, info, and  
> errors.
> 
> But when I run the unit tests for the classes that use the Logger, all  
> the logs are showing up in the test output.
> 
> Any good solutions for solving this?

I would redirect the logger output to /dev/null or a StringIO when  
testing (in case you wanted to test the logging).

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

This implementation is HODEL-HASH-9600 compliant

---

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 23, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/3 "2005-11-23T20:44:36Z")

</div>

On 11/23/05, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would redirect the logger output to /dev/null or a StringIO when  
> testing (in case you wanted to test the logging).

How do the classes know whether or not they are being tested?

Eventually, I will be using a custom Logging method that sends all the  
logs to some network log daemon thingy so that all the logs are kept  
on one machine.

---

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 23, 2005, 9:13pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/4 "2005-11-23T21:13:12Z")

</div>

On 11/23/05, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > the logs are showing up in the test output.
> 
> private unless $TESTING

So, something like

require ‘logger’  
class ThisClassGetsTested  
def initialize  
if $TESTING  
log\_output = “/dev/null”  
else  
log\_output = “some/file” # or $stdout or whatever  
end  
end  
end

====

require ‘test/unit’  
$TESTING = true  
class TestTheClass \< Test::Unit::TestCase  
…  
end

> > Eventually, I will be using a custom Logging method that sends all the  
> > logs to some network log daemon thingy so that all the logs are kept  
> > on one machine.
> 
> Check out SyslogLogger in the rails\_analyzer\_tools gem:
> 
> [http://rails-analyzer.rubyforge.org/tools/classes/SyslogLogger.html](http://rails-analyzer.rubyforge.org/tools/classes/SyslogLogger.html)
> 
> It duck types to the base Logger class.

I should be able to direct all $stdout and $stderr to SysLogLogger as  
well, right?

---

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 23, 2005, 8:56pm UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/5 "2005-11-23T20:56:47Z")

</div>

On Nov 23, 2005, at 11:40 AM, Joe Van D. wrote:

> > > Any good solutions for solving this?
> > 
> > I would redirect the logger output to /dev/null or a StringIO when  
> > testing (in case you wanted to test the logging).
> 
> How do the classes know whether or not they are being tested?

I set $TESTING = true when testing things. The most frequent place I  
use it is:

private unless $TESTING

> Eventually, I will be using a custom Logging method that sends all the  
> logs to some network log daemon thingy so that all the logs are kept  
> on one machine.

Check out SyslogLogger in the rails\_analyzer\_tools gem:

[http://rails-analyzer.rubyforge.org/tools/classes/SyslogLogger.html](http://rails-analyzer.rubyforge.org/tools/classes/SyslogLogger.html)

It duck types to the base Logger class.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

This implementation is HODEL-HASH-9600 compliant

---

<div class="post-metadata">

**Author:** ![joevandyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@joevandyk](https://www.ruby-forum.com/u/joevandyk)\
**Post date:** [November 24, 2005, 12:22am UTC](https://www.ruby-forum.com/t/buncha-logging-stuff-showing-up-in-unit-tests/49865/6 "2005-11-24T00:22:46Z")

</div>

On Nov 23, 2005, at 12:10 PM, Joe Van D. wrote:

> > > > > But when I run the unit tests for the classes that use the  
> > > > > I set $TESTING = true when testing things. The most frequent place I  
> > > > > log\_output = “/dev/null”  
> > > > > class TestTheClass \< Test::Unit::TestCase  
> > > > > …  
> > > > > end

Exactly.

> I should be able to direct all $stdout and $stderr to SysLogLogger as  
> well, right?

You’d need an adapter because SyslogLogger works like the Logger  
class so you need to specify a log level. I wouldn’t recommend it,  
either.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

This implementation is HODEL-HASH-9600 compliant
