lib/semantic_logger/base.rb in semantic_logger-4.0.0 vs lib/semantic_logger/base.rb in semantic_logger-4.1.0
- old
+ new
@@ -130,11 +130,11 @@
if thread == Thread.current
Log.cleanse_backtrace
else
log.thread_name = thread.name
log.tags = (thread[:semantic_logger_tags] || []).clone
- log.named_tags = (thread[:semantic_logger_named_tags] || []).clone
+ log.named_tags = (thread[:semantic_logger_named_tags] || {}).clone
thread.backtrace
end
# TODO: Keep backtrace instead of transforming into a text message at this point
# Maybe log_backtrace: true
if backtrace
@@ -145,26 +145,66 @@
if log.assign(message: message, backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && should_log?(log)
self.log(log)
end
end
- # :nodoc:
+ # Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active.
+ #
+ # Returns result of block.
+ #
+ # Tagged example:
+ # SemanticLogger.tagged(12345, 'jack') do
+ # logger.debug('Hello World')
+ # end
+ #
+ # Named Tags (Hash) example:
+ # SemanticLogger.tagged(tracking_number: 12345) do
+ # logger.debug('Hello World')
+ # end
+ #
+ # Notes:
+ # - Named tags are the recommended approach since the tag consists of a name value pair this is more useful
+ # than just a string value in the logs, or centralized logging system.
+ # - This method is slow when using multiple text tags since it needs to flatten the tags and
+ # remove empty elements to support Rails 4.
+ # - It is recommended to keep tags as a list without any empty values, or contain any child arrays.
+ # However, this api will convert:
+ # `logger.tagged([['first', nil], nil, ['more'], 'other'])`
+ # to:
+ # `logger.tagged('first', 'more', 'other')`
+ # - For better performance with clean tags, see `SemanticLogger.tagged`.
def tagged(*tags, &block)
- SemanticLogger.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 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?)
+ SemanticLogger.tagged(*new_tags, &block)
end
# :nodoc:
alias_method :with_tags, :tagged
# :nodoc:
def tags
SemanticLogger.tags
end
- # :nodoc:
+ # Returns the list of tags pushed after flattening them out and removing blanks
+ #
+ # Note:
+ # - This method is slow since it needs to flatten the tags and remove empty elements
+ # to support Rails 4.
+ # - For better performance with clean tags, use `SemanticLogger.push_tags`
def push_tags(*tags)
- SemanticLogger.push_tags(*tags)
+ # Need to flatten and reject empties to support calls from Rails 4
+ new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
+ SemanticLogger.push_tags(*new_tags)
end
# :nodoc:
def pop_tags(quantity = 1)
SemanticLogger.pop_tags(quantity)
@@ -173,11 +213,11 @@
# :nodoc:
def silence(new_level = :error, &block)
SemanticLogger.silence(new_level, &block)
end
- # :nodoc:
+ # Deprecated. Use `SemanticLogger.tagged`
def fast_tag(tag, &block)
SemanticLogger.fast_tag(tag, &block)
end
# :nodoc:
@@ -284,10 +324,10 @@
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) { yield(params) }
+ silence(silence_level) {yield(params)}
else
yield(params)
end
end
rescue Exception => exc