Class: J1::LogAdapter
- Inherits:
-
Object
- Object
- J1::LogAdapter
- Defined in:
- lib/j1/log_adapter.rb
Constant Summary collapse
- LOG_LEVELS =
{ :debug => ::Logger::DEBUG, :info => ::Logger::INFO, :warn => ::Logger::WARN, :error => ::Logger::ERROR, }.freeze
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
-
#abort_with(topic, message = nil) ⇒ Object
Public: Print an error message and immediately abort the process.
- #adjust_verbosity(options = {}) ⇒ Object
-
#debug(topic, message = nil) ⇒ Object
Public: Print a debug message.
-
#error(topic, message = nil) ⇒ Object
Public: Print an error message.
-
#formatted_topic(topic) ⇒ Object
Internal: Format the topic.
-
#info(topic, message = nil) ⇒ Object
Public: Print a message.
-
#initialize(writer, level = :info) ⇒ LogAdapter
constructor
Public: Create a new instance of a log writer.
-
#log_level=(level) ⇒ Object
Public: Set the log level on the writer.
-
#message(topic, message) ⇒ Object
Internal: Build a topic method.
-
#warn(topic, message = nil) ⇒ Object
Public: Print a message.
Constructor Details
#initialize(writer, level = :info) ⇒ LogAdapter
Public: Create a new instance of a log writer
writer - Logger compatible instance log_level - (optional, symbol) the log level
Returns nothing
19 20 21 22 23 |
# File 'lib/j1/log_adapter.rb', line 19 def initialize(writer, level = :info) @messages = [] @writer = writer self.log_level = level end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
3 4 5 |
# File 'lib/j1/log_adapter.rb', line 3 def @messages end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
3 4 5 |
# File 'lib/j1/log_adapter.rb', line 3 def writer @writer end |
Instance Method Details
#abort_with(topic, message = nil) ⇒ Object
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
95 96 97 98 |
# File 'lib/j1/log_adapter.rb', line 95 def abort_with(topic, = nil) error(topic, ) abort end |
#adjust_verbosity(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/j1/log_adapter.rb', line 35 def adjust_verbosity( = {}) # Quiet always wins. if [:quiet] self.log_level = :error elsif [:verbose] self.log_level = :debug end debug "Logging at level:", LOG_LEVELS.key(writer.level).to_s end |
#debug(topic, message = nil) ⇒ Object
Public: Print a debug message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
52 53 54 |
# File 'lib/j1/log_adapter.rb', line 52 def debug(topic, = nil) writer.debug((topic, )) end |
#error(topic, message = nil) ⇒ Object
Public: Print an error message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
84 85 86 |
# File 'lib/j1/log_adapter.rb', line 84 def error(topic, = nil) writer.error((topic, )) end |
#formatted_topic(topic) ⇒ Object
Internal: Format the topic
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc.
Returns the formatted topic statement
119 120 121 |
# File 'lib/j1/log_adapter.rb', line 119 def formatted_topic(topic) "#{topic} ".rjust(20) end |
#info(topic, message = nil) ⇒ Object
Public: Print a message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
63 64 65 |
# File 'lib/j1/log_adapter.rb', line 63 def info(topic, = nil) writer.info((topic, )) end |
#log_level=(level) ⇒ Object
Public: Set the log level on the writer
level - (symbol) the log level
Returns nothing
31 32 33 |
# File 'lib/j1/log_adapter.rb', line 31 def log_level=(level) writer.level = LOG_LEVELS.fetch(level) end |
#message(topic, message) ⇒ Object
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
107 108 109 110 111 |
# File 'lib/j1/log_adapter.rb', line 107 def (topic, ) msg = formatted_topic(topic) + .to_s.gsub(%r!\s+!, " ") << msg msg end |
#warn(topic, message = nil) ⇒ Object
Public: Print a message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
74 75 76 |
# File 'lib/j1/log_adapter.rb', line 74 def warn(topic, = nil) writer.warn((topic, )) end |