From 29c207ac221fef52167e4534f88a603b7f98ced4 Mon Sep 17 00:00:00 2001 From: Jordon Bedwell Date: Sat, 12 Aug 2017 18:16:20 -0500 Subject: [PATCH] Add the ability to `yield` a message. Standard loggers in Ruby mostly `yield` their messages (this is the default behavior of Ruby's own logger.) Or if there is a block then the message is the topic, a colon is automatically added, and the message is shipped with the topic. Jekyll should conform to this behavior so that it's logger can be passed to libraries that expect this standard behavior. ```ruby [1] pry(main)> Jekyll.logger.info("Just so you know") # => Just so you know [2] pry(main)> Jekyll.logger.info("Just so you know", "this is a message") # => Just so you know this is a message [3] pry(main)> Jekyll.logger.info("Just so you know") { "this is a message" } # => Just so you know: this is a message ``` --- lib/jekyll/log_adapter.rb | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/jekyll/log_adapter.rb b/lib/jekyll/log_adapter.rb index ec33b4d90d8..b5e12c3e25f 100644 --- a/lib/jekyll/log_adapter.rb +++ b/lib/jekyll/log_adapter.rb @@ -48,8 +48,8 @@ def adjust_verbosity(options = {}) # message - the message detail # # Returns nothing - def debug(topic, message = nil) - writer.debug(message(topic, message)) + def debug(topic, message = nil, &block) + writer.debug(message(topic, message, &block)) end # Public: Print a message @@ -58,8 +58,8 @@ def debug(topic, message = nil) # message - the message detail # # Returns nothing - def info(topic, message = nil) - writer.info(message(topic, message)) + def info(topic, message = nil, &block) + writer.info(message(topic, message, &block)) end # Public: Print a message @@ -69,7 +69,7 @@ def info(topic, message = nil) # # Returns nothing def warn(topic, message = nil) - writer.warn(message(topic, message)) + writer.warn(message(topic, message, &block)) end # Public: Print an error message @@ -79,7 +79,7 @@ def warn(topic, message = nil) # # Returns nothing def error(topic, message = nil) - writer.error(message(topic, message)) + writer.error(message(topic, message, &block)) end # Public: Print an error message and immediately abort the process @@ -88,8 +88,8 @@ def error(topic, message = nil) # message - the message detail (can be omitted) # # Returns nothing - def abort_with(topic, message = nil) - error(topic, message) + def abort_with(topic, message = nil, &block) + error(topic, message, &block) abort end @@ -99,10 +99,15 @@ def abort_with(topic, message = nil) # message - the message detail # # Returns the formatted message - def message(topic, message) - msg = formatted_topic(topic) + message.to_s.gsub(%r!\s+!, " ") - messages << msg - msg + def message(topic, message = nil, &block) + raise ArgumentError, "block or message, not both" if block_given? && message + + message = block.call if block_given? + message = message.to_s.gsub(%r!\s+!, " ") + topic = formatted_topic(topic, block_given?) + out = topic + message + messages << out + out end # Internal: Format the topic @@ -110,8 +115,8 @@ def message(topic, message) # topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. # # Returns the formatted topic statement - def formatted_topic(topic) - "#{topic} ".rjust(20) + def formatted_topic(topic, colon = false) + "#{topic}#{colon ? ": " : " "}".rjust(20) end end end