Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set log color explicitly #82

Merged
merged 2 commits into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions lib/itamae/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Logger
class Formatter
attr_accessor :colored
attr_accessor :depth
attr_accessor :color

INDENT_LENGTH = 3

Expand All @@ -19,7 +20,7 @@ def initialize(*args)
def call(severity, datetime, progname, msg)
log = "%s : %s%s\n" % ["%5s" % severity, ' ' * INDENT_LENGTH * depth , msg2str(msg)]
if colored
color(log, severity)
colorize(log, severity)
else
log
end
Expand All @@ -32,6 +33,14 @@ def indent
@depth -= 1
end

def color(code)
@prev_color = @color
@color = code
yield
ensure
@color = @prev_color
end

private
def msg2str(msg)
case msg
Expand All @@ -45,17 +54,21 @@ def msg2str(msg)
end
end

def color(str, severity)
color_code = case severity
when "INFO"
:green
when "WARN"
:magenta
when "ERROR"
:red
else
:clear
end
def colorize(str, severity)
if @color
color_code = @color
else
color_code = case severity
when "INFO"
:clear
when "WARN"
:magenta
when "ERROR"
:red
else
:clear
end
end
ANSI.public_send(color_code) { str }
end
end
Expand All @@ -73,7 +86,7 @@ def log_device=(value)
@log_device = value
@logger = create_logger
end

private

def create_logger
Expand Down
8 changes: 6 additions & 2 deletions lib/itamae/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,15 @@ def show_differences
if current_value.nil? && value.nil?
# ignore
elsif current_value.nil? && !value.nil?
Logger.info "#{key} will be '#{value}'"
Logger.formatter.color :green do
Logger.info "#{key} will be '#{value}'"
end
elsif current_value == value || value.nil?
Logger.debug "#{key} will not change (current value is '#{current_value}')"
else
Logger.info "#{key} will change from '#{current_value}' to '#{value}'"
Logger.formatter.color :green do
Logger.info "#{key} will change from '#{current_value}' to '#{value}'"
end
end
end
end
Expand Down