Skip to content

Commit

Permalink
Removed trace logging level and move log methods to Abstract formatter
Browse files Browse the repository at this point in the history
Trace logging was only partially implemented, e.g. the Pretty formatter did not support it. Move level specific log methods (error(‘whatever’), info(‘whatever’) etc from Abstract backend to Abstract formatter so they can be used by any method from SSHKit.config.output
  • Loading branch information
robd committed Apr 27, 2015
1 parent c92018d commit 2aa7890
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 61 deletions.
31 changes: 3 additions & 28 deletions lib/sshkit/backends/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Backend

class Abstract

extend Forwardable
def_delegators :output, :log, :fatal, :error, :warn, :info, :debug

attr_reader :host

def run
Expand All @@ -18,34 +21,6 @@ def initialize(host, &block)
@block = block
end

def log(messages)
info(messages)
end

def fatal(messages)
output << LogMessage.new(Logger::FATAL, messages)
end

def error(messages)
output << LogMessage.new(Logger::ERROR, messages)
end

def warn(messages)
output << LogMessage.new(Logger::WARN, messages)
end

def info(messages)
output << LogMessage.new(Logger::INFO, messages)
end

def debug(messages)
output << LogMessage.new(Logger::DEBUG, messages)
end

def trace(messages)
output << LogMessage.new(Logger::TRACE, messages)
end

def make(commands=[])
execute :make, commands
end
Expand Down
1 change: 0 additions & 1 deletion lib/sshkit/backends/skipper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def info(messages)
alias :fatal :info
alias :error :info
alias :debug :info
alias :trace :info

end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/sshkit/formatters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ def initialize(oio)
@original_output = oio
end

def log(messages)
info(messages)
end

def fatal(messages)
write_at_log_level(Logger::FATAL, messages)
end

def error(messages)
write_at_log_level(Logger::ERROR, messages)
end

def warn(messages)
write_at_log_level(Logger::WARN, messages)
end

def info(messages)
write_at_log_level(Logger::INFO, messages)
end

def debug(messages)
write_at_log_level(Logger::DEBUG, messages)
end

def write(obj)
raise "Abstract formatter should not be used directly, maybe you want SSHKit::Formatter::BlackHole"
end
Expand All @@ -24,6 +48,12 @@ def write(obj)
def format_std_stream_line(line)
("\t" + line).chomp
end

private

def write_at_log_level(level, messages)
write(LogMessage.new(level, messages))
end
end

end
Expand Down
1 change: 0 additions & 1 deletion lib/sshkit/logger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module SSHKit
class Logger
TRACE = -1
DEBUG = 0
INFO = 1
WARN = 2
Expand Down
17 changes: 5 additions & 12 deletions test/unit/formatters/test_pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ def teardown
end

def test_logging_fatal
assert_log("\e[0;31;49mFATAL\e[0m Test\n", Logger::FATAL, "Test")
assert_equal "\e[0;31;49mFATAL\e[0m Test\n", pretty.fatal('Test')
end

def test_logging_error
assert_log("\e[0;31;49mERROR\e[0m Test\n", Logger::ERROR, "Test")
assert_equal "\e[0;31;49mERROR\e[0m Test\n", pretty.error('Test')
end

def test_logging_warn
assert_log("\e[0;33;49mWARN\e[0m Test\n", Logger::WARN, "Test")
assert_equal "\e[0;33;49mWARN\e[0m Test\n", pretty.warn('Test')
end

def test_logging_info
assert_log("\e[0;34;49mINFO\e[0m Test\n", Logger::INFO, "Test")
assert_equal "\e[0;34;49mINFO\e[0m Test\n", pretty.info('Test')
end

def test_logging_debug
assert_log("\e[0;30;49mDEBUG\e[0m Test\n", Logger::DEBUG, "Test")
assert_equal "\e[0;30;49mDEBUG\e[0m Test\n", pretty.debug('Test')
end

def test_command_lifecycle_logging
Expand All @@ -66,12 +66,5 @@ def test_command_lifecycle_logging
assert_equal expected_log_lines, output.split("\n")
end

private

def assert_log(expected_output, level, message)
pretty << SSHKit::LogMessage.new(level, message)
assert_equal expected_output, output
end

end
end
29 changes: 11 additions & 18 deletions test/unit/formatters/test_simple_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def output
@_output ||= String.new
end

def pretty
def simple
@_simple ||= SSHKit::Formatter::SimpleText.new(output)
end

Expand All @@ -23,39 +23,39 @@ def teardown
end

def test_logging_fatal
assert_log("Test\n", Logger::FATAL, 'Test')
assert_equal "Test\n", simple.fatal('Test')
end

def test_logging_error
assert_log(output, Logger::ERROR, 'Test')
assert_equal "Test\n", simple.error('Test')
end

def test_logging_warn
assert_log(output, Logger::WARN, 'Test')
assert_equal "Test\n", simple.warn('Test')
end

def test_logging_info
assert_log(output, Logger::INFO, 'Test')
assert_equal "Test\n", simple.info('Test')
end

def test_logging_debug
assert_log(output, Logger::DEBUG, 'Test')
assert_equal "Test\n", simple.debug('Test')
end

def test_command_lifecycle_logging
command = SSHKit::Command.new(:a_cmd, 'some args', host: Host.new('localhost'))
command.stubs(:uuid).returns('aaaaaa')
command.stubs(:runtime).returns(1)

pretty << command
simple << command
command.started = true
pretty << command
simple << command
command.on_stdout('stdout message')
pretty << command
simple << command
command.on_stderr('stderr message')
pretty << command
simple << command
command.exit_status = 0
pretty << command
simple << command

expected_log_lines = [
'Running /usr/bin/env a_cmd some args on localhost',
Expand All @@ -67,12 +67,5 @@ def test_command_lifecycle_logging
assert_equal expected_log_lines, output.split("\n")
end

private

def assert_log(expected_output, level, message)
pretty << SSHKit::LogMessage.new(level, message)
assert_equal expected_output, output
end

end
end
1 change: 0 additions & 1 deletion test/unit/test_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module SSHKit
class TestLogger < UnitTest

def test_logger_severity_constants
assert_equal Logger::TRACE, -1
assert_equal Logger::DEBUG, 0
assert_equal Logger::INFO, 1
assert_equal Logger::WARN, 2
Expand Down

0 comments on commit 2aa7890

Please sign in to comment.