Class: Loggun::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/loggun/formatter.rb

Constant Summary collapse

DEFAULT_VALUE =
'-'.freeze

Instance Method Summary collapse

Instance Method Details

#call(severity, time, _program_name, message, loggun_type: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/loggun/formatter.rb', line 8

def call(severity, time, _program_name, message, loggun_type: nil)
  data = Hash.new(DEFAULT_VALUE)
  time = time.utc if config.force_utc

  process_message(data, message)

  data[:type] = loggun_type || Loggun.type || DEFAULT_VALUE.dup

  data[:timestamp] = time.iso8601(config.timestamp_precision)
  data[:time] = data[:timestamp] if config.log_format == :plain

  data[:severity] = severity&.to_s || 'INFO'
  data[:pid] = Process.pid
  data[:tags_text] = tags_text
  data[:transaction_id] = Loggun.transaction_id
  data[:parent_transaction] = parent_transaction if parent_transaction

  prepare_to_output(data)
end

#clear_tags!Object



45
46
47
# File 'lib/loggun/formatter.rb', line 45

def clear_tags!
  current_tags.clear
end

#current_tagsObject



49
50
51
52
# File 'lib/loggun/formatter.rb', line 49

def current_tags
  thread_key = @thread_key ||= "loggun_tagged_logging_tags:#{object_id}"
  Thread.current[thread_key] ||= []
end

#pop_tags(size = 1) ⇒ Object



41
42
43
# File 'lib/loggun/formatter.rb', line 41

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

#push_tags(*tags) ⇒ Object



35
36
37
38
39
# File 'lib/loggun/formatter.rb', line 35

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

#tagged(*tags) ⇒ Object



28
29
30
31
32
33
# File 'lib/loggun/formatter.rb', line 28

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

#tags_textObject



54
55
56
57
58
59
60
61
# File 'lib/loggun/formatter.rb', line 54

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