Skip to content

Commit

Permalink
handle case where string used for log device
Browse files Browse the repository at this point in the history
This works in the base logger, but Methadone was asking
the string if it was a tty, which it isn't, but doesn't even respond to,
so beefing this up.

Closes #73
  • Loading branch information
davetron5000 committed Feb 23, 2014
1 parent eaa251c commit f009486
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/methadone/cli_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ def initialize(log_device=$stdout,error_device=$stderr)
super(log_device)
@stderr_logger = Logger.new(error_device)

@split_logs = log_device.tty? && error_device.tty?
log_device_tty = tty?(log_device)
error_device_tty = tty?(error_device)

@split_logs = log_device_tty && error_device_tty

self.level = Logger::Severity::INFO
@stderr_logger.level = DEFAULT_ERROR_LEVEL

self.formatter = BLANK_FORMAT if log_device.tty?
@stderr_logger.formatter = BLANK_FORMAT if error_device.tty?
self.formatter = BLANK_FORMAT if log_device_tty
@stderr_logger.formatter = BLANK_FORMAT if error_device_tty
end

def level=(level)
Expand Down Expand Up @@ -119,5 +122,12 @@ def error_formatter=(formatter)
@stderr_logger.formatter=formatter
end

private

def tty?(device_or_string)
return device_or_string.tty? if device_or_string.respond_to? :tty?
false
end

end
end
22 changes: 22 additions & 0 deletions test/test_cli_logger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'base_test'
require 'methadone'
require 'stringio'
require 'tempfile'

class TestCLILogger < BaseTest
include Methadone
Expand Down Expand Up @@ -158,6 +159,27 @@ def tty?; true; end
}
end

test_that "we can use filenames as log devices" do
Given {
tempfile = Tempfile.new("stderr_log")
@stdout_file = tempfile.path
tempfile.close

tempfile = Tempfile.new("stdout_log")
@stderr_file = tempfile.path
tempfile.close
}
When {
@logger = CLILogger.new(@stdout_file,@stderr_file)
@logger.info("some info")
@logger.error("some error")
}
Then {
File.read(@stdout_file).should =~ /some info/
File.read(@stderr_file).should =~ /some error/
}
end

private

def log_all_levels
Expand Down

0 comments on commit f009486

Please sign in to comment.