Buncha logging stuff showing up in unit tests

J

Joe Van Dyk

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?
 
E

Eric Hodel

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).
 
J

Joe Van Dyk

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.
 
E

Eric Hodel

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

It duck types to the base Logger class.
 
J

Joe Van Dyk

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

private unless $TESTING

So, something like

require 'logger'
class ThisClassGetsTested
def initialize
if $TESTING
log_output =3D "/dev/null"
else
log_output =3D "some/file" # or $stdout or whatever
end
end
end

=3D=3D=3D=3D

require 'test/unit'
$TESTING =3D true
class TestTheClass < Test::Unit::TestCase
...
end


Check out SyslogLogger in the rails_analyzer_tools gem:

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?
 
E

Eric Hodel

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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top