Module: RailsLogstasher::TaggedLogging::Formatter

Defined in:
lib/rails-logstasher/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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails-logstasher/tagged_logging.rb', line 21

def call(severity, timestamp, progname, msg)
  @entry = nil
  if msg.class == RailsLogstasher::Event
    @entry = msg
  else
    @entry = RailsLogstasher::Event.new(Rails.logger)
    @entry.message = msg
  end
  @entry.fields['severity'] = severity
  @entry.type = @log_type
  process_tags(current_tags)
  process_tags(current_request_tags)

  process_entry

  #TODO Should we do anything with progname? What about source?
  super(severity, timestamp, progname, @entry.to_json) unless @entry.cancelled?
end

#clear_tags!Object



78
79
80
81
# File 'lib/rails-logstasher/tagged_logging.rb', line 78

def clear_tags!
  current_request_tags.clear
  current_tags.clear
end

#current_request_tagsObject



87
88
89
# File 'lib/rails-logstasher/tagged_logging.rb', line 87

def current_request_tags
  Thread.current[:activesupport_tagged_logging_request_tags] ||= []
end

#current_tagsObject



83
84
85
# File 'lib/rails-logstasher/tagged_logging.rb', line 83

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

#log_entry_processor=(log_entry_processor) ⇒ Object



95
96
97
# File 'lib/rails-logstasher/tagged_logging.rb', line 95

def log_entry_processor=(log_entry_processor)
  @log_entry_processor = log_entry_processor
end

#log_type=(log_type) ⇒ Object



91
92
93
# File 'lib/rails-logstasher/tagged_logging.rb', line 91

def log_type=(log_type)
  @log_type = log_type
end

#pop_tags(size = 1) ⇒ Object



74
75
76
# File 'lib/rails-logstasher/tagged_logging.rb', line 74

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

#process_entryObject



40
41
42
43
# File 'lib/rails-logstasher/tagged_logging.rb', line 40

def process_entry
  return unless @log_entry_processor && @log_entry_processor.class == Proc
  @log_entry_processor.call @entry
end

#process_tags(tags) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails-logstasher/tagged_logging.rb', line 62

def process_tags(tags)
  tags.each do |tag|
    if tag.class == Hash
      tag.each_pair do |k,v|
        @entry.fields[k] = v
      end
    else
      @entry.tags << tag
    end
  end
end

#push_request_tags(tags) ⇒ Object



58
59
60
# File 'lib/rails-logstasher/tagged_logging.rb', line 58

def push_request_tags(tags)
  Thread.current[:activesupport_tagged_logging_request_tags] = tags
end

#push_tags(*tags) ⇒ Object



52
53
54
55
56
# File 'lib/rails-logstasher/tagged_logging.rb', line 52

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

#tagged(*tags) ⇒ Object



45
46
47
48
49
50
# File 'lib/rails-logstasher/tagged_logging.rb', line 45

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