Set logging to debug when verbose flag is set

Adds Jekyll::LogAdapter#adjust_verbosity which ensures that --quiet
always wins.
This commit is contained in:
Jim Meyer 2015-04-18 16:57:57 -07:00
parent f908051aa4
commit 9c03fc3f27
4 changed files with 55 additions and 3 deletions

View File

@ -23,7 +23,8 @@ module Jekyll
# Build your jekyll site # Build your jekyll site
# Continuously watch if `watch` is set to true in the config. # Continuously watch if `watch` is set to true in the config.
def process(options) def process(options)
Jekyll.logger.log_level = :error if options['quiet'] # Adjust verbosity quickly
Jekyll.logger.adjust_verbosity(options)
options = configuration_from_options(options) options = configuration_from_options(options)
site = Jekyll::Site.new(options) site = Jekyll::Site.new(options)

View File

@ -54,6 +54,7 @@ module Jekyll
'timezone' => nil, # use the local timezone 'timezone' => nil, # use the local timezone
'quiet' => false, 'quiet' => false,
'verbose' => false,
'defaults' => [], 'defaults' => [],
'rdiscount' => { 'rdiscount' => {
@ -103,6 +104,10 @@ module Jekyll
override['quiet'] || self['quiet'] || DEFAULTS['quiet'] override['quiet'] || self['quiet'] || DEFAULTS['quiet']
end end
def verbose?(override = {})
override['verbose'] || self['verbose'] || DEFAULTS['verbose']
end
def safe_load_file(filename) def safe_load_file(filename)
case File.extname(filename) case File.extname(filename)
when /\.toml/i when /\.toml/i
@ -121,8 +126,8 @@ module Jekyll
# #
# Returns an Array of config files # Returns an Array of config files
def config_files(override) def config_files(override)
# Be quiet quickly. # Adjust verbosity quickly
Jekyll.logger.log_level = :error if quiet?(override) Jekyll.logger.adjust_verbosity(:quiet => quiet?(override), :verbose => verbose?(override))
# Get configuration from <source>/_config.yml or <source>/<config_file> # Get configuration from <source>/_config.yml or <source>/<config_file>
config_files = override.delete('config') config_files = override.delete('config')

View File

@ -30,6 +30,16 @@ module Jekyll
writer.level = LOG_LEVELS.fetch(level) writer.level = LOG_LEVELS.fetch(level)
end end
def adjust_verbosity(options = {})
# Quiet always wins.
if options[:quiet]
self.log_level = :error
elsif options[:verbose]
self.log_level = :debug
end
debug "Logging at level:", LOG_LEVELS.key(writer.level).to_s
end
# Public: Print a debug message # Public: Print a debug message
# #
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. # topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.

View File

@ -18,6 +18,42 @@ class TestLogAdapter < JekyllUnitTest
end end
end end
context "#adjust_verbosity" do
should "set the writers logging level to error when quiet" do
subject = Jekyll::LogAdapter.new(LoggerDouble.new)
subject.adjust_verbosity(:quiet => true)
assert_equal Jekyll::LogAdapter::LOG_LEVELS[:error], subject.writer.level
end
should "set the writers logging level to debug when verbose" do
subject = Jekyll::LogAdapter.new(LoggerDouble.new)
subject.adjust_verbosity(:verbose => true)
assert_equal Jekyll::LogAdapter::LOG_LEVELS[:debug], subject.writer.level
end
should "set the writers logging level to error when quiet and verbose are both set" do
subject = Jekyll::LogAdapter.new(LoggerDouble.new)
subject.adjust_verbosity(:quiet => true, :verbose => true)
assert_equal Jekyll::LogAdapter::LOG_LEVELS[:error], subject.writer.level
end
should "not change the writer's logging level when neither verbose or quiet" do
subject = Jekyll::LogAdapter.new(LoggerDouble.new)
original_level = subject.writer.level
refute_equal Jekyll::LogAdapter::LOG_LEVELS[:error], subject.writer.level
refute_equal Jekyll::LogAdapter::LOG_LEVELS[:debug], subject.writer.level
subject.adjust_verbosity(:quiet => false, :verbose => false)
assert_equal original_level, subject.writer.level
end
should "call #debug on writer return true" do
writer = LoggerDouble.new
logger = Jekyll::LogAdapter.new(writer)
allow(writer).to receive(:debug).with('Logging at level: '.rjust(20) + 'info').and_return(true)
assert logger.adjust_verbosity
end
end
context "#debug" do context "#debug" do
should "call #debug on writer return true" do should "call #debug on writer return true" do
writer = LoggerDouble.new writer = LoggerDouble.new