Module: ActiveSupport::TaggedLogging::Formatter

Defined in:
lib/active_support/tagged_logging.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#call(severity, timestamp, progname, msg) ⇒ Object

This method is invoked when a log event occurs.



22
23
24
# File 'lib/active_support/tagged_logging.rb', line 22

def call(severity, timestamp, progname, msg)
  super(severity, timestamp, progname, "#{tags_text}#{msg}")
end

#clear_tags!Object



43
44
45
# File 'lib/active_support/tagged_logging.rb', line 43

def clear_tags!
  current_tags.clear
end

#current_tagsObject



47
48
49
50
51
# File 'lib/active_support/tagged_logging.rb', line 47

def current_tags
  # We use our object ID here to avoid conflicting with other instances
  thread_key = @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}".freeze
  Thread.current[thread_key] ||= []
end

#pop_tags(size = 1) ⇒ Object



39
40
41
# File 'lib/active_support/tagged_logging.rb', line 39

def pop_tags(size = 1)
  current_tags.pop size
end

#push_tags(*tags) ⇒ Object



33
34
35
36
37
# File 'lib/active_support/tagged_logging.rb', line 33

def push_tags(*tags)
  tags.flatten.reject(&:blank?).tap do |new_tags|
    current_tags.concat new_tags
  end
end

#tagged(*tags) ⇒ Object



26
27
28
29
30
31
# File 'lib/active_support/tagged_logging.rb', line 26

def tagged(*tags)
  new_tags = push_tags(*tags)
  yield self
ensure
  pop_tags(new_tags.size)
end

#tags_textObject



53
54
55
56
57
58
# File 'lib/active_support/tagged_logging.rb', line 53

def tags_text
  tags = current_tags
  if tags.any?
    tags.collect { |tag| "[#{tag}] " }.join
  end
end