Class: Async::Logger
- Inherits:
-
Object
- Object
- Async::Logger
- Defined in:
- lib/async/logger.rb
Defined Under Namespace
Classes: Buffer
Constant Summary collapse
- LEVELS =
{debug: 0, info: 1, warn: 2, error: 3, fatal: 4}
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
- #disable(subject) ⇒ Object
- #enable(subject) ⇒ Object
- #enabled?(subject) ⇒ Boolean
- #format(subject = nil, *arguments, &block) ⇒ Object
- #format_argument(argument, output: @output) ⇒ Object
- #format_exception(exception, prefix = nil, pwd: Dir.pwd, output: @output) ⇒ Object
- #format_subject(prefix, subject, output: @output) ⇒ Object
- #format_value(value, output: @output) ⇒ Object
-
#initialize(output, level: 1) ⇒ Logger
constructor
A new instance of Logger.
- #log(level, *arguments, &block) ⇒ Object
- #time_offset_prefix ⇒ Object
Constructor Details
#initialize(output, level: 1) ⇒ Logger
Returns a new instance of Logger.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/async/logger.rb', line 62 def initialize(output, level: 1) @output = output @level = level @start = Time.now @terminal = Terminal.new(output) @reset_style = @terminal.reset @prefix_style = @terminal.color(Terminal::Colors::CYAN) @subject_style = @terminal.color(nil, nil, Terminal::Attributes::BOLD) @exception_title_style = @terminal.color(Terminal::Colors::RED, nil, Terminal::Attributes::BOLD) @exception_details_style = @terminal.color(Terminal::Colors::YELLOW) @exception_line_style = @terminal.color(Terminal::Colors::RED) @subjects = {} end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
78 79 80 |
# File 'lib/async/logger.rb', line 78 def level @level end |
Instance Method Details
#disable(subject) ⇒ Object
96 97 98 |
# File 'lib/async/logger.rb', line 96 def disable(subject) @subjects[subject.class] = false end |
#enable(subject) ⇒ Object
92 93 94 |
# File 'lib/async/logger.rb', line 92 def enable(subject) @subjects[subject.class] = true end |
#enabled?(subject) ⇒ Boolean
88 89 90 |
# File 'lib/async/logger.rb', line 88 def enabled?(subject) @subjects[subject.class] == true end |
#format(subject = nil, *arguments, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/async/logger.rb', line 108 def format(subject = nil, *arguments, &block) prefix = time_offset_prefix indent = " " * prefix.size buffer = Buffer.new("#{indent}| ") if subject format_subject(prefix, subject, output: buffer) end arguments.each do |argument| format_argument(argument, output: buffer) end if block_given? if block.arity.zero? format_argument(yield, output: buffer) else yield(buffer, @terminal) end end @output.write buffer.string end |
#format_argument(argument, output: @output) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/async/logger.rb', line 133 def format_argument(argument, output: @output) if argument.is_a? Exception format_exception(argument, output: output) else format_value(argument, output: output) end end |
#format_exception(exception, prefix = nil, pwd: Dir.pwd, output: @output) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/async/logger.rb', line 141 def format_exception(exception, prefix = nil, pwd: Dir.pwd, output: @output) lines = exception..lines.map(&:chomp) output.puts " #{prefix}#{@exception_title_style}#{exception.class}#{@reset_style}: #{lines.shift}" lines.each do |line| output.puts " #{@exception_details_style}" + line + @reset_style end exception.backtrace.each_with_index do |line, index| path, offset, = line.split(":") # Make the path a bit more readable path.gsub!(/^#{pwd}\//, "./") output.puts " #{index == 0 ? "→" : " "} #{@exception_line_style}#{path}:#{offset}#{@reset_style} #{}" end if exception.cause format_exception(exception.cause, "Caused by ", pwd: pwd, output: output) end end |
#format_subject(prefix, subject, output: @output) ⇒ Object
164 165 166 |
# File 'lib/async/logger.rb', line 164 def format_subject(prefix, subject, output: @output) output.puts "#{@subject_style}#{subject}#{@reset_style}", prefix: "#{@prefix_style}#{prefix}: " end |
#format_value(value, output: @output) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/async/logger.rb', line 168 def format_value(value, output: @output) string = value.to_s string.each_line do |line| output.puts "#{line}" end end |
#log(level, *arguments, &block) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/async/logger.rb', line 100 def log(level, *arguments, &block) unless level.is_a? Symbol level = LEVELS[level] end self.send(level, *arguments, &block) end |
#time_offset_prefix ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/async/logger.rb', line 176 def time_offset_prefix offset = Time.now - @start minutes = (offset/60).floor seconds = (offset - (minutes*60)) if minutes > 0 "#{minutes}m#{seconds.floor}s" else "#{seconds.round(2)}s" end.rjust(6) end |