Class: TaggedLogging::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/tagged_logging/formatter.rb

Constant Summary collapse

FORMAT =
"[%s] - %-5s - %s - %s\n"

Instance Method Summary collapse

Instance Method Details

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



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tagged_logging/formatter.rb', line 9

def call(severity, time, progname, msg)
  str = case msg
        when ::String
          msg
        when ::Exception
          "#{ msg.message } (#{ msg.class })\n    | " <<
            (msg.backtrace || []).join("\n    | ")
        else
          msg.inspect
        end
  FORMAT % [format_datetime(time), severity, tags_text, msg]
end

#clear_tags!Object



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

def clear_tags!
  current_tags.clear
end

#current_tagsObject



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

def current_tags
  Thread.current[:__tagged_logging_current_tags] ||= []
end

#pop_tags(size = 1) ⇒ Object



35
36
37
# File 'lib/tagged_logging/formatter.rb', line 35

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

#push_tags(*tags) ⇒ Object



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

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

#tagged(*tags) ⇒ Object



22
23
24
25
26
27
# File 'lib/tagged_logging/formatter.rb', line 22

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

#tags_textObject



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

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