X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=lib%2Fmcollective%2Flogger%2Fconsole_logger.rb;fp=lib%2Fmcollective%2Flogger%2Fconsole_logger.rb;h=9d5b8eac63330f64f1e3f36baba93859c259d758;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/logger/console_logger.rb b/lib/mcollective/logger/console_logger.rb new file mode 100644 index 0000000..9d5b8ea --- /dev/null +++ b/lib/mcollective/logger/console_logger.rb @@ -0,0 +1,59 @@ +module MCollective + module Logger + # Implements a syslog based logger using the standard ruby syslog class + class Console_logger :info, + :warn => :warning, + :debug => :debug, + :fatal => :crit, + :error => :err} + end + + def log(level, from, msg, normal_output=STDERR, last_resort_output=STDERR) + time = Time.new.strftime("%Y/%m/%d %H:%M:%S") + + normal_output.puts("%s %s: %s %s" % [colorize(level, level), time, from, msg]) + rescue + # if this fails we probably cant show the user output at all, + # STDERR it as last resort + last_resort_output.puts("#{level}: #{msg}") + end + + # Set some colors for various logging levels, will honor the + # color configuration option and return nothing if its configured + # not to + def color(level) + colorize = Config.instance.color + + colors = {:error => Util.color(:red), + :fatal => Util.color(:red), + :warn => Util.color(:yellow), + :info => Util.color(:green), + :reset => Util.color(:reset)} + + if colorize + return colors[level] || "" + else + return "" + end + end + + # Helper to return a string in specific color + def colorize(level, msg) + "%s%s%s" % [ color(level), msg, color(:reset) ] + end + end + end +end