Class: Capissh::Logger
- Inherits:
-
Object
- Object
- Capissh::Logger
- Defined in:
- lib/capissh/logger.rb
Constant Summary collapse
- IMPORTANT =
0
- INFO =
1
- DEBUG =
2
- TRACE =
3
- MAX_LEVEL =
3
- COLORS =
{ :none => "0", :black => "30", :red => "31", :green => "32", :yellow => "33", :blue => "34", :magenta => "35", :cyan => "36", :white => "37" }
- STYLES =
{ :bright => 1, :dim => 2, :underscore => 4, :blink => 5, :reverse => 7, :hidden => 8 }
Instance Attribute Summary collapse
-
#device ⇒ Object
Returns the value of attribute device.
-
#disable_formatters ⇒ Object
Returns the value of attribute disable_formatters.
-
#level ⇒ Object
Returns the value of attribute level.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #debug(message, line_prefix = nil) ⇒ Object
- #format(message, color, style, nl = "\n") ⇒ Object
- #important(message, line_prefix = nil) ⇒ Object
- #info(message, line_prefix = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Logger
constructor
A new instance of Logger.
- #log(level, message, line_prefix = nil) ⇒ Object
- #trace(message, line_prefix = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Logger
Returns a new instance of Logger.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/capissh/logger.rb', line 69 def initialize(={}) output = [:output] || $stderr if output.respond_to?(:puts) @device = output else @device = File.open(output.to_str, "a") @needs_close = true end @options = @level = [:level] || 0 @disable_formatters = [:disable_formatters] end |
Instance Attribute Details
#device ⇒ Object
Returns the value of attribute device.
3 4 5 |
# File 'lib/capissh/logger.rb', line 3 def device @device end |
#disable_formatters ⇒ Object
Returns the value of attribute disable_formatters.
3 4 5 |
# File 'lib/capissh/logger.rb', line 3 def disable_formatters @disable_formatters end |
#level ⇒ Object
Returns the value of attribute level.
3 4 5 |
# File 'lib/capissh/logger.rb', line 3 def level @level end |
Class Method Details
.add_formatter(options) ⇒ Object
54 55 56 57 |
# File 'lib/capissh/logger.rb', line 54 def add_formatter() @formatters.push() @sorted_formatters = nil end |
.default ⇒ Object
64 65 66 |
# File 'lib/capissh/logger.rb', line 64 def default @default ||= new(:level => 3) end |
.sorted_formatters ⇒ Object
59 60 61 62 |
# File 'lib/capissh/logger.rb', line 59 def sorted_formatters # Sort matchers in reverse order so we can break if we found a match. @sorted_formatters ||= @formatters.sort_by { |i| -(i[:priority] || i[:prio] || 0) } end |
Instance Method Details
#close ⇒ Object
83 84 85 |
# File 'lib/capissh/logger.rb', line 83 def close device.close if @needs_close end |
#debug(message, line_prefix = nil) ⇒ Object
144 145 146 |
# File 'lib/capissh/logger.rb', line 144 def debug(, line_prefix=nil) log(DEBUG, , line_prefix) end |
#format(message, color, style, nl = "\n") ⇒ Object
152 153 154 155 |
# File 'lib/capissh/logger.rb', line 152 def format(, color, style, nl = "\n") style = "#{style};" if style "\e[#{style}#{color}m" + .to_s.strip + "\e[0m#{nl}" end |
#important(message, line_prefix = nil) ⇒ Object
136 137 138 |
# File 'lib/capissh/logger.rb', line 136 def important(, line_prefix=nil) log(IMPORTANT, , line_prefix) end |
#info(message, line_prefix = nil) ⇒ Object
140 141 142 |
# File 'lib/capissh/logger.rb', line 140 def info(, line_prefix=nil) log(INFO, , line_prefix) end |
#log(level, message, line_prefix = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/capissh/logger.rb', line 87 def log(level, , line_prefix=nil) if level <= self.level # Only format output if device is a TTY and formatters are not disabled if device.tty? && !@disable_formatters color = :none style = nil Logger.sorted_formatters.each do |formatter| if (formatter[:level] == level || formatter[:level].nil?) if =~ formatter[:match] || line_prefix =~ formatter[:match] color = formatter[:color] if formatter[:color] style = formatter[:style] || formatter[:attribute] # (support original cap colors) .gsub!(formatter[:match], formatter[:replace]) if formatter[:replace] = formatter[:prepend] + unless formatter[:prepend].nil? = + formatter[:append] unless formatter[:append].nil? = Time.now.strftime('%Y-%m-%d %T') + ' ' + if formatter[:timestamp] break unless formatter[:replace] end end end if color == :hide # Don't do anything if color is set to :hide return false end term_color = COLORS[color] term_style = STYLES[style] # Don't format message if no color or style unless color == :none and style.nil? unless line_prefix.nil? line_prefix = format(line_prefix, term_color, term_style, nil) end = format(, term_color, term_style) end end indent = "%*s" % [MAX_LEVEL, "*" * (MAX_LEVEL - level)] (RUBY_VERSION >= "1.9" ? .lines : ).each do |line| if line_prefix device.puts "#{indent} [#{line_prefix}] #{line.strip}\n" else device.puts "#{indent} #{line.strip}\n" end end end end |
#trace(message, line_prefix = nil) ⇒ Object
148 149 150 |
# File 'lib/capissh/logger.rb', line 148 def trace(, line_prefix=nil) log(TRACE, , line_prefix) end |