104 lines
2.6 KiB
Ruby
104 lines
2.6 KiB
Ruby
require 'logger'
|
|
module Jekyll
|
|
class LogWriter
|
|
attr_reader :writer
|
|
|
|
LOG_LEVELS = {
|
|
debug: 0,
|
|
info: 1,
|
|
warn: 2,
|
|
error: 3
|
|
}
|
|
|
|
# Public: Create a new instance of Jekyll's log writer
|
|
#
|
|
# writer - Logger compatible instance
|
|
# log_level - (optional, symbol) the log level
|
|
#
|
|
# Returns nothing
|
|
def initialize(writer, level = :info)
|
|
@writer = writer
|
|
self.log_level = level
|
|
end
|
|
|
|
# Public: Set the log level on the writer
|
|
#
|
|
# level - (symbol) the log level
|
|
#
|
|
# Returns nothing
|
|
def log_level=(level)
|
|
writer.level = LOG_LEVELS.fetch(level)
|
|
end
|
|
|
|
# Public: Print a jekyll debug message
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
# message - the message detail
|
|
#
|
|
# Returns nothing
|
|
def debug(topic, message = nil)
|
|
writer.debug(message(topic, message))
|
|
end
|
|
|
|
# Public: Print a jekyll message
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
# message - the message detail
|
|
#
|
|
# Returns nothing
|
|
def info(topic, message = nil)
|
|
writer.info(message(topic, message))
|
|
end
|
|
|
|
# Public: Print a jekyll message
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
# message - the message detail
|
|
#
|
|
# Returns nothing
|
|
def warn(topic, message = nil)
|
|
writer.warn(message(topic, message).yellow)
|
|
end
|
|
|
|
# Public: Print a jekyll error message
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
# message - the message detail
|
|
#
|
|
# Returns nothing
|
|
def error(topic, message = nil)
|
|
writer.error(message(topic, message).red)
|
|
end
|
|
|
|
# Public: Print a Jekyll 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
|
|
def abort_with(topic, message = nil)
|
|
error(topic, message)
|
|
abort
|
|
end
|
|
|
|
# Public: Build a Jekyll topic method
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
# message - the message detail
|
|
#
|
|
# Returns the formatted message
|
|
def message(topic, message)
|
|
formatted_topic(topic) + message.to_s.gsub(/\s+/, ' ')
|
|
end
|
|
|
|
# Public: Format the topic
|
|
#
|
|
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
#
|
|
# Returns the formatted topic statement
|
|
def formatted_topic(topic)
|
|
"#{topic} ".rjust(20)
|
|
end
|
|
end
|
|
end
|