class J1::LogAdapter

Constants

LOG_LEVELS

Attributes

messages[R]
writer[R]

Public Class Methods

new(writer, level = :info) click to toggle source

Public: Create a new instance of a log writer

writer - Logger compatible instance log_level - (optional, symbol) the log level

Returns nothing

# File lib/j1/log_adapter.rb, line 18
def initialize(writer, level = :info)
  @messages = []
  @writer = writer
  self.log_level = level
end

Public Instance Methods

abort_with(topic, message = nil) click to toggle source

Public: Print an error message and immediately abort the process

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail (can be omitted)

Returns nothing

# File lib/j1/log_adapter.rb, line 89
def abort_with(topic, message = nil)
  error(topic, message)
  abort
end
adjust_verbosity(options = {}) click to toggle source
# File lib/j1/log_adapter.rb, line 33
def adjust_verbosity(options = {})
  # Quiet always wins.
  if options[:quiet]
    self.log_level = :error
  elsif options[:verbose]
    self.log_level = :debug
  end
  debug "Logging at level:", LOG_LEVELS.key(writer.level).to_s
end
debug(topic, message = nil) click to toggle source

Public: Print a debug message

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing

# File lib/j1/log_adapter.rb, line 49
def debug(topic, message = nil)
  writer.debug(message(topic, message))
end
error(topic, message = nil) click to toggle source

Public: Print an error message

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing

# File lib/j1/log_adapter.rb, line 79
def error(topic, message = nil)
  writer.error(message(topic, message))
end
formatted_topic(topic) click to toggle source

Internal: Format the topic

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc.

Returns the formatted topic statement

# File lib/j1/log_adapter.rb, line 111
def formatted_topic(topic)
  "#{topic} ".rjust(20)
end
info(topic, message = nil) click to toggle source

Public: Print a message

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing

# File lib/j1/log_adapter.rb, line 59
def info(topic, message = nil)
  writer.info(message(topic, message))
end
log_level=(level) click to toggle source

Public: Set the log level on the writer

level - (symbol) the log level

Returns nothing

# File lib/j1/log_adapter.rb, line 29
def log_level=(level)
  writer.level = LOG_LEVELS.fetch(level)
end
message(topic, message) click to toggle source

Internal: Build a topic method

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns the formatted message

# File lib/j1/log_adapter.rb, line 100
def message(topic, message)
  msg = formatted_topic(topic) + message.to_s.gsub(%r!\s+!, " ")
  messages << msg
  msg
end
warn(topic, message = nil) click to toggle source

Public: Print a message

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns nothing

# File lib/j1/log_adapter.rb, line 69
def warn(topic, message = nil)
  writer.warn(message(topic, message))
end