From 5e0e5885b83edee362e954ee10dcf4c46c2ef7c7 Mon Sep 17 00:00:00 2001 From: Thom May Date: Mon, 26 Feb 2018 15:52:40 +0000 Subject: Resolve review comments Signed-off-by: Thom May --- lib/mixlib/log.rb | 17 ++++++++++++----- lib/mixlib/log/child.rb | 17 ++++++++++++----- lib/mixlib/log/logger.rb | 3 +++ lib/mixlib/log/logging.rb | 29 +++++++---------------------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb index 5bcb71d..7193583 100644 --- a/lib/mixlib/log.rb +++ b/lib/mixlib/log.rb @@ -121,11 +121,9 @@ module Mixlib # loggers that may have been added, even though it is possible to configure # two (or more) loggers at different log levels. [:trace?, :debug?, :info?, :warn?, :error?, :fatal?].each do |method_name| - class_eval(<<-METHOD_DEFN, __FILE__, __LINE__) - def #{method_name} - logger.#{method_name} - end - METHOD_DEFN + define_method(method_name) do + logger.send(method_name) + end end def <<(msg) @@ -148,6 +146,15 @@ module Mixlib alias :log :add + def with_child(metadata = {}) + child = Child.new(self, metadata) + if block_given? + yield child + else + child + end + end + # Passes any other method calls on directly to the underlying Logger object created with init. If # this method gets hit before a call to Mixlib::Logger.init has been made, it will call # Mixlib::Logger.init() with no arguments. diff --git a/lib/mixlib/log/child.rb b/lib/mixlib/log/child.rb index 8f33daf..971c956 100644 --- a/lib/mixlib/log/child.rb +++ b/lib/mixlib/log/child.rb @@ -36,11 +36,9 @@ module Mixlib # loggers that may have been added, even though it is possible to configure # two (or more) loggers at different log levels. [:trace?, :debug?, :info?, :warn?, :error?, :fatal?].each do |method_name| - class_eval(<<-METHOD_DEFN, __FILE__, __LINE__) - def #{method_name} - parent.send(:#{method_name}) - end - METHOD_DEFN + define_method(method_name) do + parent.send(method_name) + end end def add(severity, message = nil, progname = nil, data: {}, &block) @@ -48,6 +46,15 @@ module Mixlib parent.send(:pass, severity, message, progname, data: data, &block) end + def with_child(metadata = {}) + child = Child.new(self, metadata) + if block_given? + yield child + else + child + end + end + end end end diff --git a/lib/mixlib/log/logger.rb b/lib/mixlib/log/logger.rb index c97be8e..abc7bc3 100644 --- a/lib/mixlib/log/logger.rb +++ b/lib/mixlib/log/logger.rb @@ -1,4 +1,5 @@ require "logger" +require "mixlib/log/logging" # A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff # ripped out, and metadata added in. @@ -6,6 +7,7 @@ module Mixlib module Log class Logger < ::Logger + include Logging # # === Synopsis # @@ -50,6 +52,7 @@ module Mixlib format_message(format_severity(severity), Time.now, progname, data)) true end + alias_method :add, :add_data class LocklessLogDevice < LogDevice diff --git a/lib/mixlib/log/logging.rb b/lib/mixlib/log/logging.rb index be84527..537e1de 100644 --- a/lib/mixlib/log/logging.rb +++ b/lib/mixlib/log/logging.rb @@ -15,23 +15,19 @@ # limitations under the License. require "logger" -require "mixlib/log/child" module Mixlib module Log module Logging + include ::Logger::Severity - module Severity - include ::Logger::Severity - TRACE = -1 + TRACE = -1 - SEV_LABEL = %w{TRACE DEBUG INFO WARN ERROR FATAL ANY}.each(&:freeze).freeze + SEV_LABEL = %w{TRACE DEBUG INFO WARN ERROR FATAL ANY}.each(&:freeze).freeze - def to_label(sev) - SEV_LABEL[sev + 1] || -"ANY" - end + def to_label(sev) + SEV_LABEL[sev + 1] || -"ANY" end - include Severity LEVELS = { :trace => TRACE, :debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR, :fatal => FATAL }.freeze LEVEL_NAMES = LEVELS.invert.freeze @@ -47,19 +43,8 @@ module Mixlib # No need to incur method_missing overhead on every log call. [:trace, :debug, :info, :warn, :error, :fatal].each do |method_name| level = LEVELS[method_name] - class_eval(<<-METHOD_DEFN, __FILE__, __LINE__) - def #{method_name}(msg=nil, data: {}, &block) - pass(#{level}, msg, data: data, &block) - end - METHOD_DEFN - end - - def with_child(metadata = {}) - child = Child.new(self, metadata) - if block_given? - yield child - else - child + define_method(method_name) do |msg = nil, data: {}, &block| + pass(level, msg, data: data, &block) end end -- cgit v1.2.1