Class: AMQPLogging::MetricsAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp_logging/metrics_agent.rb

Defined Under Namespace

Modules: MetricsAgentSupport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsAgent

Returns a new instance of MetricsAgent.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/amqp_logging/metrics_agent.rb', line 8

def initialize
  @default_fields = {
    :host => Socket.gethostname.split('.').first,
    :pid => Process.pid,
    :loglines => {
      :default => []
    }
  }
  @logger_types = {}
  reset_fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/amqp_logging/metrics_agent.rb', line 6

def fields
  @fields
end

Instance Method Details

#[](fieldname) ⇒ Object



35
36
37
# File 'lib/amqp_logging/metrics_agent.rb', line 35

def [](fieldname)
  @fields[fieldname]
end

#[]=(fieldname, value) ⇒ Object



39
40
41
# File 'lib/amqp_logging/metrics_agent.rb', line 39

def []=(fieldname, value)
  @fields[fieldname] = value
end

#add_logline(severity, message, progname, logger) ⇒ Object



43
44
45
46
47
# File 'lib/amqp_logging/metrics_agent.rb', line 43

def add_logline(severity, message, progname, logger)
  t = Time.now
  msg = (message || progname).strip
  @fields[:loglines][@logger_types[logger]] << [severity, t.strftime("%Y-%d-%mT%H:%M:%S.#{t.usec}"), msg]
end

#flushObject



30
31
32
33
# File 'lib/amqp_logging/metrics_agent.rb', line 30

def flush
  logger.info(@fields.to_json + "\n")
  reset_fields
end

#loggerObject



20
21
22
# File 'lib/amqp_logging/metrics_agent.rb', line 20

def logger
  @logger || (self.logger = ::Logger.new($stdout))
end

#logger=(logger) ⇒ Object



24
25
26
27
28
# File 'lib/amqp_logging/metrics_agent.rb', line 24

def logger=(logger)
  @logger = logger
  @logger.formatter = Proc.new {|_, _, msg, progname| msg || progname}
  @logger
end

#wrap_logger(logger, type = :default) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/amqp_logging/metrics_agent.rb', line 49

def wrap_logger(logger, type = :default)
  agent = self
  register_logger(logger, type)
  logger.instance_eval do
    @agent = agent
    class << self
      include MetricsAgentSupport
    end
  end
  logger
end