Class: Bridgetown::LogAdapter
- Inherits:
-
Object
- Object
- Bridgetown::LogAdapter
- Defined in:
- bridgetown-core/lib/bridgetown-core/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
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#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
Print an error message and immediately abort the process.
-
#adjust_verbosity(options = {}) ⇒ Object
-
#debug(topic, message = nil) ⇒ Object
Print a debug message.
-
#error(topic, message = nil) ⇒ Object
Print an error message.
-
#formatted_topic(topic, colon = false) ⇒ String
Format the topic.
-
#info(topic, message = nil) ⇒ Object
Print an informational message.
-
#initialize(writer, level = :info) ⇒ LogAdapter
constructor
Create a new instance of a log writer.
-
#log_level=(level) ⇒ Object
Set the log level on the writer.
-
#message(topic, message = nil) ⇒ String
Build a topic method.
-
#warn(topic, message = nil) ⇒ Object
Print a warning message.
-
#write(level_of_message, topic, message = nil) ⇒ BasicObject
Log a message.
-
#write_message?(level_of_message) ⇒ Boolean
Check if the message should be written given the log level.
Constructor Details
#initialize(writer, level = :info) ⇒ LogAdapter
Create a new instance of a log writer
18 19 20 21 22 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 18 def initialize(writer, level = :info) @messages = [] @writer = writer self.log_level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
5 6 7 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 5 def level @level end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
5 6 7 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 5 def @messages end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
5 6 7 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 5 def writer @writer end |
Instance Method Details
#abort_with(topic, message = nil) ⇒ Object
Print an error message and immediately abort the process
80 81 82 83 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 80 def abort_with(topic, = nil, &) error(topic, , &) abort end |
#adjust_verbosity(options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 34 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
Print a debug message
48 49 50 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 48 def debug(topic, = nil, &) write(:debug, topic, , &) end |
#error(topic, message = nil) ⇒ Object
Print an error message
72 73 74 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 72 def error(topic, = nil, &) write(:error, topic, , &) end |
#formatted_topic(topic, colon = false) ⇒ String
Format the topic
106 107 108 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 106 def formatted_topic(topic, colon = false) # rubocop:disable Style/OptionalBooleanParameter "#{topic}#{colon ? ": " : " "}".rjust(20) end |
#info(topic, message = nil) ⇒ Object
Print an informational message
56 57 58 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 56 def info(topic, = nil, &) write(:info, topic, , &) end |
#log_level=(level) ⇒ Object
Set the log level on the writer
27 28 29 30 31 32 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 27 def log_level=(level) writer.level = level if level.is_a?(Integer) && level.between?(0, 3) writer.level = LOG_LEVELS[level] || raise(ArgumentError, "unknown log level") @level = level end |
#message(topic, message = nil) ⇒ String
Build a topic method
90 91 92 93 94 95 96 97 98 99 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 90 def (topic, = nil) raise ArgumentError, "block or message, not both" if block_given? && = yield if block_given? = .to_s.gsub(%r!\s+!, " ") topic = formatted_topic(topic, block_given?) out = topic + << out out end |
#warn(topic, message = nil) ⇒ Object
Print a warning message
64 65 66 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 64 def warn(topic, = nil, &) write(:warn, topic, , &) end |
#write(level_of_message, topic, message = nil) ⇒ BasicObject
Log a message. If a block is provided containing the message, use that instead.
125 126 127 128 129 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 125 def write(, topic, = nil, &) return false unless () writer.public_send(, (topic, , &)) end |
#write_message?(level_of_message) ⇒ Boolean
Check if the message should be written given the log level
114 115 116 |
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 114 def () LOG_LEVELS.fetch(level) <= LOG_LEVELS.fetch() end |