lib/semantic_logger/base.rb in semantic_logger-2.20.0 vs lib/semantic_logger/base.rb in semantic_logger-2.21.0

- old
+ new

@@ -106,10 +106,19 @@ else block.call(params) if block end end + # If the tag being supplied is definitely a string then this fast + # tag api can be used for short lived tags + def fast_tag(tag) + (Thread.current[:semantic_logger_tags] ||= []) << tag + yield + ensure + Thread.current[:semantic_logger_tags].pop + end + # Add the supplied tags to the list of tags to log for this thread whilst # the supplied block is active # Returns nil if no tags are currently set # To support: ActiveSupport::TaggedLogging V3 and above def tagged(*tags) @@ -288,11 +297,11 @@ @filter.call(struct) == true end end # Log message at the specified level - def log_internal(level, index, message=nil, payload=nil, exception=nil, &block) + def log_internal(level, index, message=nil, payload=nil, exception=nil) # Detect exception being logged if exception.nil? && payload.nil? && message.kind_of?(Exception) exception = message message = nil elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message) @@ -301,11 +310,11 @@ exception = payload payload = nil end # Add result of block as message or payload if not nil - if block && (result = block.call) + if block_given? && (result = yield) if result.is_a?(String) message = message.nil? ? result : "#{message} -- #{result}" elsif payload && payload.respond_to?(:merge) payload.merge(result) else @@ -318,11 +327,11 @@ payload = payload.nil? ? self.payload : self.payload.merge(payload) end # Add caller stack trace backtrace = - if !exception && (index >= SemanticLogger.backtrace_level_index) + if index >= SemanticLogger.backtrace_level_index trace = caller # Remove call to this internal method trace.shift(1) trace end @@ -330,21 +339,21 @@ struct = Log.new(level, Thread.current.name, name, message, payload, Time.now, nil, tags, index, exception, nil, backtrace) log(struct) if include_message?(struct) end # Measure the supplied block and log the message - def benchmark_internal(level, index, message, params, &block) + def benchmark_internal(level, index, message, params) start = Time.now exception = nil begin - if block + if block_given? result = if silence_level = params[:silence] # In case someone accidentally sets `silence: true` instead of `silence: :error` silence_level = :error if silence_level == true - silence(silence_level) { block.call(params) } + silence(silence_level) { yield(params) } else - block.call(params) + yield(params) end exception = params[:exception] result end rescue Exception => exc