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)
end

#clear_tags!Object



80
81
82
83
# File 'lib/rails-logstasher/tagged_logging.rb', line 80

def clear_tags!
  current_request_tags.clear
  current_tags.clear
end

#current_request_tagsObject



89
90
91
# File 'lib/rails-logstasher/tagged_logging.rb', line 89

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

#current_tagsObject



85
86
87
# File 'lib/rails-logstasher/tagged_logging.rb', line 85

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

#log_type=(log_type) ⇒ Object



93
94
95
# File 'lib/rails-logstasher/tagged_logging.rb', line 93

def log_type=(log_type)
  @log_type = log_type
end

#pop_tags(size = 1) ⇒ Object



76
77
78
# File 'lib/rails-logstasher/tagged_logging.rb', line 76

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

#process_entryObject



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

def process_entry
  entry_processor = RailsLogstasher.config[:entry_processor]
  return unless entry_processor && entry_processor.class == Proc

  entry_processor.call @entry
end

#process_tags(tags) ⇒ Object



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

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



60
61
62
# File 'lib/rails-logstasher/tagged_logging.rb', line 60

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

#push_tags(*tags) ⇒ Object



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

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

#tagged(*tags) ⇒ Object



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

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