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 rotated skipped updated ].freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the current log level (debug, info, warn, error).
-
#logdev ⇒ Object
writeonly
Sets the log device.
-
#max_length ⇒ Object
writeonly
Max length of log messages (truncate in middle).
-
#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_change(tags_added: [], tags_removed: [], count: 1, item: nil, single: false) ⇒ Object
-
#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.
-
#restore_level ⇒ Object
Restore temporary level.
-
#temp_level(level) ⇒ Object
Set log level temporarily.
-
#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
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/doing/log_adapter.rb', line 48 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 @prev_level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the current log level (debug, info, warn, error)
15 16 17 |
# File 'lib/doing/log_adapter.rb', line 15 def level @level end |
#logdev=(value) ⇒ Object (writeonly)
Sets the log device
9 10 11 |
# File 'lib/doing/log_adapter.rb', line 9 def logdev=(value) @logdev = value end |
#max_length=(value) ⇒ Object (writeonly)
Max length of log messages (truncate in middle)
12 13 14 |
# File 'lib/doing/log_adapter.rb', line 12 def max_length=(value) @max_length = value end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
17 18 19 |
# File 'lib/doing/log_adapter.rb', line 17 def end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
17 18 19 |
# File 'lib/doing/log_adapter.rb', line 17 def results @results end |
Instance Method Details
#abort_with(topic, message = nil, &block) ⇒ Object
Print an error message and immediately abort the process
186 187 188 189 |
# File 'lib/doing/log_adapter.rb', line 186 def abort_with(topic, = nil, &block) error(topic, , &block) abort end |
#adjust_verbosity(options = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/doing/log_adapter.rb', line 99 def adjust_verbosity( = {}) if [:log_level] self.log_level = [:log_level].to_sym elsif [: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
111 112 113 114 115 116 117 118 |
# File 'lib/doing/log_adapter.rb', line 111 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
128 129 130 |
# File 'lib/doing/log_adapter.rb', line 128 def debug(topic, = nil, &block) write(:debug, topic, , &block) end |
#error(topic, message = nil, &block) ⇒ Object
Print an error message
170 171 172 |
# File 'lib/doing/log_adapter.rb', line 170 def error(topic, = nil, &block) write(:error, topic, , &block) end |
#formatted_topic(topic, colon: false) ⇒ Object
Format the topic
201 202 203 204 205 206 207 208 209 |
# File 'lib/doing/log_adapter.rb', line 201 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
142 143 144 |
# File 'lib/doing/log_adapter.rb', line 142 def info(topic, = nil, &block) write(:info, topic, , &block) end |
#log_change(tags_added: [], tags_removed: [], count: 1, item: nil, single: false) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/doing/log_adapter.rb', line 268 def log_change(tags_added: [], tags_removed: [], count: 1, item: nil, single: false) if .empty? && .empty? count(:skipped, level: :debug, message: '%count %items with no change', count: count) else if .empty? count(:skipped, level: :debug, message: 'no tags added to %count %items') elsif single && item added = . info('Tagged:', %(added #{tags_added.count == 1 ? 'tag' : 'tags'} #{added} to #{item.title})) else count(:added_tags, level: :info, tag: , message: '%tags added to %count %items') end if .empty? count(:skipped, level: :debug, message: 'no tags removed from %count %items') elsif single && item added = . info('Untagged:', %(removed #{tags_removed.count == 1 ? 'tag' : 'tags'} #{added} from #{item.title})) else count(:removed_tags, level: :info, tag: , message: '%tags removed from %count %items') end end end |
#log_level=(level = 'info') ⇒ Object
Set the log level on the writer
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/doing/log_adapter.rb', line 66 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
240 241 242 243 244 245 246 247 248 |
# File 'lib/doing/log_adapter.rb', line 240 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
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/doing/log_adapter.rb', line 255 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 |
#restore_level ⇒ Object
Restore temporary level
92 93 94 95 96 97 |
# File 'lib/doing/log_adapter.rb', line 92 def restore_level return if @prev_level.nil? || @prev_level == @log_level self.log_level = @prev_level @prev_level = nil end |
#temp_level(level) ⇒ Object
Set log level temporarily
84 85 86 87 88 89 |
# File 'lib/doing/log_adapter.rb', line 84 def temp_level(level) return if level.nil? || level.to_sym == @log_level @prev_level = log_level.dup @log_level = level.to_sym end |
#warn(topic, message = nil, &block) ⇒ Object
Print a message
156 157 158 |
# File 'lib/doing/log_adapter.rb', line 156 def warn(topic, = nil, &block) write(:warn, topic, , &block) end |
#write(level_of_message, topic, message = nil, &block) ⇒ Boolean
Log a message.
227 228 229 230 |
# File 'lib/doing/log_adapter.rb', line 227 def write(, topic, = nil, &block) @results << { level: , message: (topic, , &block) } true end |