lib/semantic_logger/base.rb in semantic_logger-4.6.1 vs lib/semantic_logger/base.rb in semantic_logger-4.7.0

- old
+ new

@@ -124,11 +124,11 @@ alias benchmark measure # Log a thread backtrace def backtrace(thread: Thread.current, level: :warn, - message: 'Backtrace:', + message: "Backtrace:", payload: nil, metric: nil, metric_amount: nil) log = Log.new(name, level) @@ -188,11 +188,12 @@ # - For better performance with clean tags, see `SemanticLogger.tagged`. def tagged(*tags, &block) # Allow named tags to be passed into the logger if tags.size == 1 tag = tags[0] - return yield if tag.nil? || tag == '' + return yield if tag.nil? || tag == "" + return tag.is_a?(Hash) ? SemanticLogger.named_tagged(tag, &block) : SemanticLogger.fast_tag(tag.to_s, &block) end # Need to flatten and reject empties to support calls from Rails 4 new_tags = tags.flatten.collect(&:to_s).reject(&:empty?) @@ -238,11 +239,11 @@ SemanticLogger.fast_tag(tag, &block) end # Write log data to underlying data storage def log(_log_) - raise NotImplementedError, 'Logging Appender must implement #log(log)' + raise NotImplementedError, "Logging Appender must implement #log(log)" end # Whether this log entry meets the criteria to be logged by this appender. def should_log?(log) meets_log_level?(log) && !filtered?(log) @@ -267,11 +268,11 @@ # regular expression. All other messages will be ignored # Proc: Only include log messages where the supplied Proc returns true # The Proc must return true or false def initialize(klass, level = nil, filter = nil) # Support filtering all messages to this logger using a Regular Expression or Proc - raise ':filter must be a Regexp or Proc' unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc) + raise ":filter must be a Regexp or Proc" unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc) @filter = filter.is_a?(Regexp) ? filter.freeze : filter @name = klass.is_a?(String) ? klass : klass.name if level.nil? # Allow the global default level to determine this loggers log level @@ -306,16 +307,16 @@ log = Log.new(name, level, index) should_log = if payload.nil? && exception.nil? && message.is_a?(Hash) # Check if someone just logged a hash payload instead of meaning to call semantic logger if message.key?(:message) || message.key?(:payload) || message.key?(:exception) || message.key?(:metric) - log.assign(message) + log.assign(**message) else log.assign_positional(nil, message, nil, &block) end elsif exception.nil? && message && payload && payload.is_a?(Hash) && - (payload.key?(:payload) || payload.key?(:exception) || payload.key?(:metric)) + (payload.key?(:payload) || payload.key?(:exception) || payload.key?(:metric)) log.assign(message: message, **payload) else log.assign_positional(message, payload, exception, &block) end @@ -342,22 +343,22 @@ silence(silence_level) { yield(params) } else yield(params) end end - rescue Exception => exc - exception = exc + rescue Exception => e + exception = e ensure # Must use ensure block otherwise a `return` in the yield above will skip the log entry - log = Log.new(name, level, index) + log = Log.new(name, level, index) exception ||= params[:exception] - message = params[:message] if params[:message] - duration = + message = params[:message] if params[:message] + duration = if block_given? 1_000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) else - params[:duration] || raise('Mandatory block missing when :duration option is not supplied') + params[:duration] || raise("Mandatory block missing when :duration option is not supplied") end # Extract options after block completes so that block can modify any of the options payload = params[:payload] @@ -375,10 +376,11 @@ ) # Log level may change during assign due to :on_exception_level self.log(log) if should_log && should_log?(log) raise exception if exception + result end end # For measuring methods and logging their duration. @@ -393,11 +395,11 @@ # Ignores filter, silence, payload exception = nil start = Process.clock_gettime(Process::CLOCK_MONOTONIC) begin yield - rescue Exception => exc - exception = exc + rescue Exception => e + exception = e ensure log = Log.new(name, level, index) # May return false due to elastic logging should_log = log.assign( message: message,