Class: Jekyll::LogAdapter
- Inherits:
-
Object
- Object
- Jekyll::LogAdapter
- Defined in:
- lib/jekyll/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, &block) ⇒ Object
Public: Print an error message and immediately abort the process.
- #adjust_verbosity(options = {}) ⇒ Object
-
#debug(topic, message = nil, &block) ⇒ Object
Public: Print a debug message.
-
#error(topic, message = nil, &block) ⇒ Object
Public: Print an error message.
-
#formatted_topic(topic, colon = false) ⇒ Object
Internal: Format the topic.
-
#info(topic, message = nil, &block) ⇒ 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 = nil) ⇒ Object
Internal: Build a topic method.
-
#warn(topic, message = nil, &block) ⇒ Object
Public: Print a message.
-
#write(level_of_message, topic, message = nil, &block) ⇒ Object
Internal: Log a message.
-
#write_message?(level_of_message) ⇒ Boolean
Internal: Check if the message should be written given the log level.
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
20 21 22 23 24 |
# File 'lib/jekyll/log_adapter.rb', line 20 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 'lib/jekyll/log_adapter.rb', line 5 def level @level end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
5 6 7 |
# File 'lib/jekyll/log_adapter.rb', line 5 def @messages end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
5 6 7 |
# File 'lib/jekyll/log_adapter.rb', line 5 def writer @writer end |
Instance Method Details
#abort_with(topic, message = nil, &block) ⇒ 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
92 93 94 95 |
# File 'lib/jekyll/log_adapter.rb', line 92 def abort_with(topic, = nil, &block) error(topic, , &block) abort end |
#adjust_verbosity(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/jekyll/log_adapter.rb', line 36 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, &block) ⇒ 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/jekyll/log_adapter.rb', line 52 def debug(topic, = nil, &block) write(:debug, topic, , &block) end |
#error(topic, message = nil, &block) ⇒ Object
Public: Print an error message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
82 83 84 |
# File 'lib/jekyll/log_adapter.rb', line 82 def error(topic, = nil, &block) write(:error, topic, , &block) end |
#formatted_topic(topic, colon = false) ⇒ Object
Internal: Format the topic
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. colon -
Returns the formatted topic statement
120 121 122 |
# File 'lib/jekyll/log_adapter.rb', line 120 def formatted_topic(topic, colon = false) "#{topic}#{colon ? ": " : " "}".rjust(20) end |
#info(topic, message = nil, &block) ⇒ Object
Public: Print a message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
62 63 64 |
# File 'lib/jekyll/log_adapter.rb', line 62 def info(topic, = nil, &block) write(:info, topic, , &block) end |
#log_level=(level) ⇒ Object
Public: Set the log level on the writer
level - (symbol) the log level
Returns nothing
31 32 33 34 |
# File 'lib/jekyll/log_adapter.rb', line 31 def log_level=(level) writer.level = LOG_LEVELS.fetch(level) @level = level end |
#message(topic, message = nil) ⇒ 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
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jekyll/log_adapter.rb', line 103 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, &block) ⇒ Object
Public: Print a message
topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail
Returns nothing
72 73 74 |
# File 'lib/jekyll/log_adapter.rb', line 72 def warn(topic, = nil, &block) write(:warn, topic, , &block) end |
#write(level_of_message, topic, message = nil, &block) ⇒ Object
Internal: Log a message.
level_of_message - the Symbol level of message, one of :debug, :info, :warn, :error topic - the String topic or full message message - the String message (optional) block - a block containing the message (optional)
Returns false if the message was not written, otherwise returns the value of calling the appropriate writer method, e.g. writer.info.
142 143 144 145 |
# File 'lib/jekyll/log_adapter.rb', line 142 def write(, topic, = nil, &block) return false unless () writer.public_send(, (topic, , &block)) end |
#write_message?(level_of_message) ⇒ Boolean
Internal: Check if the message should be written given the log level.
level_of_message - the Symbol level of message, one of :debug, :info, :warn, :error
Returns whether the message should be written.
129 130 131 |
# File 'lib/jekyll/log_adapter.rb', line 129 def () LOG_LEVELS.fetch(level) <= LOG_LEVELS.fetch() end |