Class: ServerspecLogstashFormatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/serverspec_logstash_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ ServerspecLogstashFormatter

Returns a new instance of ServerspecLogstashFormatter.



10
11
12
13
14
15
# File 'lib/serverspec_logstash_formatter.rb', line 10

def initialize(output)
  super
  @output_hash = {
    :version => RSpec::Core::Version::STRING
  }
end

Instance Attribute Details

#output_hashObject (readonly)

Returns the value of attribute output_hash.



8
9
10
# File 'lib/serverspec_logstash_formatter.rb', line 8

def output_hash
  @output_hash
end

Instance Method Details

#close(_notification) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/serverspec_logstash_formatter.rb', line 36

def close(_notification)
  @output_hash.each do | entry |
      output.write entry.to_json
      output.write "\n"
  end
  output.close if IO === output && output != $stdout
end

#message(notification) ⇒ Object



17
18
19
# File 'lib/serverspec_logstash_formatter.rb', line 17

def message(notification)
  (@output_hash[:messages] ||= []) << notification.message
end

#stop(notification) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/serverspec_logstash_formatter.rb', line 21

def stop(notification)
  @output_hash = notification.examples.map do |example|
    format_example(example).tap do |hash|
      e = example.exception
      if e
        hash[:exception] =  {
          :class => e.class.name,
          :message => e.message,
          :backtrace => e.backtrace,
        }
      end
    end
  end
end