Class: Doing::LogAdapter
- Inherits:
-
Object
- Object
- Doing::LogAdapter
- Defined in:
- lib/doing/log_adapter.rb
Overview
Log adapter
Constant Summary collapse
- TOPIC_WIDTH =
12- LOG_LEVELS =
{ debug: ::Logger::DEBUG, info: ::Logger::INFO, warn: ::Logger::WARN, error: ::Logger::ERROR }.freeze
- COUNT_KEYS =
i[ added archived autotag completed completed_archived deleted moved skipped updated ].freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#logdev ⇒ Object
writeonly
Sets the attribute logdev.
-
#max_length ⇒ Object
writeonly
Sets the attribute max_length.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#abort_with(topic, message = nil, &block) ⇒ Object
Print an error message and immediately abort the process.
- #adjust_verbosity(options = {}) ⇒ Object
- #count(key, level: :info, count: 1, tag: nil, message: nil) ⇒ Object
-
#debug(topic, message = nil, &block) ⇒ Object
Print a debug message.
-
#error(topic, message = nil, &block) ⇒ Object
Print an error message.
-
#formatted_topic(topic, colon: false) ⇒ Object
Format the topic.
-
#info(topic, message = nil, &block) ⇒ Object
Print a message.
-
#initialize(level = :info) ⇒ LogAdapter
constructor
Create a new instance of a log writer.
-
#log_level=(level = 'info') ⇒ Object
Set the log level on the writer.
-
#log_now(level, topic, message = nil, &block) ⇒ Object
Log to console immediately instead of writing messages on exit.
-
#output_results ⇒ Object
Output registers based on log level.
-
#warn(topic, message = nil, &block) ⇒ Object
Print a message.
-
#write(level_of_message, topic, message = nil, &block) ⇒ Boolean
Log a message.
Constructor Details
#initialize(level = :info) ⇒ LogAdapter
Create a new instance of a log writer
40 41 42 43 44 45 46 47 48 |
# File 'lib/doing/log_adapter.rb', line 40 def initialize(level = :info) = [] @counters = {} COUNT_KEYS.each { |key| @counters[key] = { tag: [], count: 0 } } @results = [] @logdev = $stderr @max_length = `tput cols`.strip.to_i - 5 || 85 self.log_level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
10 11 12 |
# File 'lib/doing/log_adapter.rb', line 10 def level @level end |
#logdev=(value) ⇒ Object (writeonly)
Sets the attribute logdev
8 9 10 |
# File 'lib/doing/log_adapter.rb', line 8 def logdev=(value) @logdev = value end |
#max_length=(value) ⇒ Object (writeonly)
Sets the attribute max_length
8 9 10 |
# File 'lib/doing/log_adapter.rb', line 8 def max_length=(value) @max_length = value end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
10 11 12 |
# File 'lib/doing/log_adapter.rb', line 10 def end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
10 11 12 |
# File 'lib/doing/log_adapter.rb', line 10 def results @results end |
Instance Method Details
#abort_with(topic, message = nil, &block) ⇒ Object
Print an error message and immediately abort the process
159 160 161 162 |
# File 'lib/doing/log_adapter.rb', line 159 def abort_with(topic, = nil, &block) error(topic, , &block) abort end |
#adjust_verbosity(options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/doing/log_adapter.rb', line 74 def adjust_verbosity( = {}) if [:quiet] self.log_level = :error elsif [:verbose] || [:debug] self.log_level = :debug end log_now :debug, 'Logging at level:', @level.to_s # log_now :debug, 'Doing Version:', Doing::VERSION end |
#count(key, level: :info, count: 1, tag: nil, message: nil) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/doing/log_adapter.rb', line 84 def count(key, level: :info, count: 1, tag: nil, message: nil) raise ArgumentError, 'invalid counter key' unless COUNT_KEYS.include?(key) @counters[key][:count] += count @counters[key][:tag].concat(tag).sort.uniq unless tag.nil? @counters[key][:level] ||= level @counters[key][:message] ||= end |
#debug(topic, message = nil, &block) ⇒ Object
Print a debug message
101 102 103 |
# File 'lib/doing/log_adapter.rb', line 101 def debug(topic, = nil, &block) write(:debug, topic, , &block) end |
#error(topic, message = nil, &block) ⇒ Object
Print an error message
143 144 145 |
# File 'lib/doing/log_adapter.rb', line 143 def error(topic, = nil, &block) write(:error, topic, , &block) end |
#formatted_topic(topic, colon: false) ⇒ Object
Format the topic
174 175 176 177 178 179 180 181 182 |
# File 'lib/doing/log_adapter.rb', line 174 def formatted_topic(topic, colon: false) if colon "#{topic}: ".rjust(TOPIC_WIDTH) elsif topic =~ /:$/ "#{topic} ".rjust(TOPIC_WIDTH) else "#{topic} " end end |
#info(topic, message = nil, &block) ⇒ Object
Print a message
115 116 117 |
# File 'lib/doing/log_adapter.rb', line 115 def info(topic, = nil, &block) write(:info, topic, , &block) end |
#log_level=(level = 'info') ⇒ Object
Set the log level on the writer
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/doing/log_adapter.rb', line 57 def log_level=(level = 'info') level = level.to_s level = case level when /^[e0]/i :error when /^[w1]/i :warn when /^[d3]/i :debug else :info end @level = level end |
#log_now(level, topic, message = nil, &block) ⇒ Object
Log to console immediately instead of writing messages on exit
213 214 215 216 217 218 219 220 221 |
# File 'lib/doing/log_adapter.rb', line 213 def log_now(level, topic, = nil, &block) return false unless (level) if @logdev == $stdout @logdev.puts (topic, , &block) else @logdev.puts (level, topic, , &block) end end |
#output_results ⇒ Object
Output registers based on log level
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/doing/log_adapter.rb', line 228 def output_results total_counters results = @results.select { |msg| (msg[:level]) }.uniq if @logdev == $stdout $stdout.print results.map {|res| res[:message].uncolor }.join("\n") else results.each do |msg| @logdev.puts (msg[:level], msg[:message]) end end end |
#warn(topic, message = nil, &block) ⇒ Object
Print a message
129 130 131 |
# File 'lib/doing/log_adapter.rb', line 129 def warn(topic, = nil, &block) write(:warn, topic, , &block) end |
#write(level_of_message, topic, message = nil, &block) ⇒ Boolean
Log a message.
200 201 202 203 |
# File 'lib/doing/log_adapter.rb', line 200 def write(, topic, = nil, &block) @results << { level: , message: (topic, , &block) } true end |