Class: ActiveLogger::Formatters::Base

Inherits:
ActiveSupport::Logger::Formatter
  • Object
show all
Defined in:
lib/active_logger/formatters/base.rb

Overview

:nodoc:

Direct Known Subclasses

Default, Json, Syslog

Instance Method Summary collapse

Instance Method Details

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



8
9
10
# File 'lib/active_logger/formatters/base.rb', line 8

def call(severity, timestamp, progname, msg)
  super(severity, timestamp, progname, msg)
end

#clear_tags!Object



32
33
34
35
# File 'lib/active_logger/formatters/base.rb', line 32

def clear_tags!
  @tags_text = nil
  current_tags.clear
end

#current_tagsObject



37
38
39
40
41
# File 'lib/active_logger/formatters/base.rb', line 37

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

#default_prognameObject



58
59
60
# File 'lib/active_logger/formatters/base.rb', line 58

def default_progname
  $PROGRAM_NAME
end

#pidObject



54
55
56
# File 'lib/active_logger/formatters/base.rb', line 54

def pid
  $PID
end

#pop_tags(size = 1) ⇒ Object



27
28
29
30
# File 'lib/active_logger/formatters/base.rb', line 27

def pop_tags(size = 1)
  @tags_text = nil
  current_tags.pop size
end

#push_tags(*tags) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/active_logger/formatters/base.rb', line 19

def push_tags(*tags)
  @tags_text = nil
  tags.flatten!
  tags.reject!(&:blank?)
  current_tags.concat tags
  tags
end

#tagged(*tags) ⇒ Object



12
13
14
15
16
17
# File 'lib/active_logger/formatters/base.rb', line 12

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

#tags_textObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/active_logger/formatters/base.rb', line 43

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