Module: Kafka::TaggedFormatter

Defined in:
lib/kafka/tagged_logger.rb

Instance Method Summary collapse

Instance Method Details

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



9
10
11
# File 'lib/kafka/tagged_logger.rb', line 9

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

#clear_tags!Object



30
31
32
# File 'lib/kafka/tagged_logger.rb', line 30

def clear_tags!
  current_tags.clear
end

#current_tagsObject



34
35
36
37
38
# File 'lib/kafka/tagged_logger.rb', line 34

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

#pop_tags(size = 1) ⇒ Object



26
27
28
# File 'lib/kafka/tagged_logger.rb', line 26

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

#push_tags(*tags) ⇒ Object



20
21
22
23
24
# File 'lib/kafka/tagged_logger.rb', line 20

def push_tags(*tags)
  tags.flatten.reject { |t| t.nil? || t.empty? }.tap do |new_tags|
    current_tags.concat new_tags
  end
end

#tagged(*tags) ⇒ Object



13
14
15
16
17
18
# File 'lib/kafka/tagged_logger.rb', line 13

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

#tags_textObject



40
41
42
43
44
45
# File 'lib/kafka/tagged_logger.rb', line 40

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