Allow Jekyll's logger to be set to any Logger compatible instance
* Stevenson now inherits from Logger and extends some methods to use $stderr for log messages greater than info level. * LogWriter provides an interface between Jekyll and Logger to maintain API.
This commit is contained in:
parent
8d65c9c92f
commit
4a73e099b7
|
@ -31,6 +31,7 @@ require 'toml'
|
||||||
# internal requires
|
# internal requires
|
||||||
require 'jekyll/version'
|
require 'jekyll/version'
|
||||||
require 'jekyll/utils'
|
require 'jekyll/utils'
|
||||||
|
require 'jekyll/log_writer'
|
||||||
require 'jekyll/stevenson'
|
require 'jekyll/stevenson'
|
||||||
require 'jekyll/deprecator'
|
require 'jekyll/deprecator'
|
||||||
require 'jekyll/configuration'
|
require 'jekyll/configuration'
|
||||||
|
@ -115,7 +116,11 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.logger
|
def self.logger
|
||||||
@logger ||= Stevenson.new
|
@logger ||= LogWriter.new(Stevenson.new)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.logger=(writer)
|
||||||
|
@logger = LogWriter.new(writer)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: File system root
|
# Public: File system root
|
||||||
|
|
|
@ -0,0 +1,103 @@
|
||||||
|
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
|
|
@ -1,102 +1,49 @@
|
||||||
|
require 'logger'
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class Stevenson
|
class Stevenson < ::Logger
|
||||||
attr_accessor :log_level
|
def initialize
|
||||||
|
@progname = nil
|
||||||
LOG_LEVELS = {
|
@level = DEBUG
|
||||||
debug: 0,
|
@default_formatter = Formatter.new
|
||||||
info: 1,
|
@logdev = $stdout
|
||||||
warn: 2,
|
@formatter = proc do |severity, datetime, progname, msg|
|
||||||
error: 3
|
"#{msg}"
|
||||||
}
|
end
|
||||||
|
|
||||||
# Public: Create a new instance of Stevenson, Jekyll's logger
|
|
||||||
#
|
|
||||||
# level - (optional, symbol) the log level
|
|
||||||
#
|
|
||||||
# Returns nothing
|
|
||||||
def initialize(level = :info)
|
|
||||||
@log_level = level
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Print a jekyll debug message to stdout
|
def add(severity, message = nil, progname = nil, &block)
|
||||||
#
|
severity ||= UNKNOWN
|
||||||
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
@logdev = set_logdevice(severity)
|
||||||
# message - the message detail
|
|
||||||
#
|
if @logdev.nil? or severity < @level
|
||||||
# Returns nothing
|
return true
|
||||||
def debug(topic, message = nil)
|
end
|
||||||
$stdout.puts(message(topic, message)) if should_log(:debug)
|
progname ||= @progname
|
||||||
|
if message.nil?
|
||||||
|
if block_given?
|
||||||
|
message = yield
|
||||||
|
else
|
||||||
|
message = progname
|
||||||
|
progname = @progname
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@logdev.puts(
|
||||||
|
format_message(format_severity(severity), Time.now, progname, message))
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Print a jekyll message to stdout
|
def close
|
||||||
#
|
# No LogDevice in use
|
||||||
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
||||||
# message - the message detail
|
|
||||||
#
|
|
||||||
# Returns nothing
|
|
||||||
def info(topic, message = nil)
|
|
||||||
$stdout.puts(message(topic, message)) if should_log(:info)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Print a jekyll message to stderr
|
private
|
||||||
#
|
|
||||||
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
|
||||||
# message - the message detail
|
|
||||||
#
|
|
||||||
# Returns nothing
|
|
||||||
def warn(topic, message = nil)
|
|
||||||
$stderr.puts(message(topic, message).yellow) if should_log(:warn)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Public: Print a jekyll error message to stderr
|
def set_logdevice(severity)
|
||||||
#
|
if severity > INFO
|
||||||
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
|
$stderr
|
||||||
# message - the message detail
|
else
|
||||||
#
|
$stdout
|
||||||
# Returns nothing
|
|
||||||
def error(topic, message = nil)
|
|
||||||
$stderr.puts(message(topic, message).red) if should_log(:error)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Print a Jekyll error message to stderr 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
|
|
||||||
|
|
||||||
# Public: Determine whether the current log level warrants logging at the
|
|
||||||
# proposed level.
|
|
||||||
#
|
|
||||||
# level_of_message - the log level of the message (symbol)
|
|
||||||
#
|
|
||||||
# Returns true if the log level of the message is greater than or equal to
|
|
||||||
# this logger's log level.
|
|
||||||
def should_log(level_of_message)
|
|
||||||
LOG_LEVELS.fetch(log_level) <= LOG_LEVELS.fetch(level_of_message)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue