Skip to content

Commit

Permalink
Merge pull request theforeman#12 from mbacovsky/logger_watch
Browse files Browse the repository at this point in the history
Added watch method to logger for easier debugging
  • Loading branch information
mbacovsky committed Aug 15, 2013
2 parents 4335c39 + 7f7728d commit bc6821b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/cli_config.template.yml
Expand Up @@ -6,6 +6,9 @@
:username: 'admin'
:password: 'changeme'

# :watch_plain: true disables color output of logger.watch in Clamp commands
:watch_plain: false

#:log_dir: '/var/log/foreman'
:log_dir: '~/.foreman/log'
:log_level: 'error'
1 change: 1 addition & 0 deletions hammer_cli.gemspec
Expand Up @@ -27,5 +27,6 @@ EOF
s.add_dependency 'terminal-table'
s.add_dependency 'rest-client'
s.add_dependency 'logging'
s.add_dependency 'awesome_print'

end
3 changes: 3 additions & 0 deletions lib/hammer_cli/abstract.rb
@@ -1,5 +1,6 @@
require 'hammer_cli/autocompletion'
require 'hammer_cli/exception_handler'
require 'hammer_cli/logger_watch'
require 'clamp'
require 'logging'

Expand Down Expand Up @@ -53,6 +54,8 @@ def exception_handler

def logger name=self.class
logger = Logging.logger[name]
logger.extend(HammerCLI::Logger::Watch) if not logger.respond_to? :watch
logger
end

def validator
Expand Down
1 change: 1 addition & 0 deletions lib/hammer_cli/apipie/read_command.rb
Expand Up @@ -16,6 +16,7 @@ def output

def execute
d = retrieve_data
logger.watch "Retrieved data: ", d
print_records d
return 0
end
Expand Down
14 changes: 14 additions & 0 deletions lib/hammer_cli/logger_watch.rb
@@ -0,0 +1,14 @@
require 'awesome_print'

module HammerCLI
module Logger
module Watch
def watch(label, obj, options={})
if debug?
options = { :plain => HammerCLI::Settings[:watch_plain], :indent => -2 }.merge(options)
debug label + "\n" + obj.ai(options)
end
end
end
end
end
40 changes: 40 additions & 0 deletions test/unit/abstract_test.rb
Expand Up @@ -78,6 +78,46 @@ def execute
@log_output.read.must_include "ERROR My logger : Test"
end

class TestLogCmd3 < HammerCLI::AbstractCommand
def execute
logger.watch "Test", {}
0
end
end

it "should have logger that can inspect object" do
test_command = Class.new(TestLogCmd3).new("")
test_command.run []
@log_output.read.must_include "DEBUG TestLogCmd3 : Test\n{}"
end

class TestLogCmd4 < HammerCLI::AbstractCommand
def execute
logger.watch "Test", { :a => 'a' }, { :plain => true }
0
end
end

it "should have logger.watch output without colors" do
test_command = Class.new(TestLogCmd4).new("")
test_command.run []
@log_output.read.must_include "DEBUG TestLogCmd4 : Test\n{\n :a => \"a\"\n}"
end

class TestLogCmd5 < HammerCLI::AbstractCommand
def execute
logger.watch "Test", { :a => 'a' }
0
end
end

it "should have logger.watch colorized output switch in settings" do
test_command = Class.new(TestLogCmd5).new("")
HammerCLI::Settings.clear
HammerCLI::Settings.load(:watch_plain => true)
test_command.run []
@log_output.read.must_include "DEBUG TestLogCmd5 : Test\n{\n :a => \"a\"\n}"
end
end

end
Expand Down

0 comments on commit bc6821b

Please sign in to comment.