Class: LogStash::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/event.rb

Overview

General event type. Will expand this in the future.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = Hash.new) ⇒ Event

Returns a new instance of Event.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/logstash/event.rb', line 9

def initialize(data=Hash.new)
  @cancelled = false
  @data = {
    "@source" => "unknown",
    "@type" => nil,
    "@tags" => [],
    "@fields" => {},
  }.merge(data)

  if !@data.include?("@timestamp")
    @data["@timestamp"] = LogStash::Time.now.utc.to_iso8601
  end
end

Class Method Details

.from_json(json) ⇒ Object



24
25
26
# File 'lib/logstash/event.rb', line 24

def self.from_json(json)
  return LogStash::Event.new(JSON.parse(json))
end

Instance Method Details

#==(other) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/logstash/event.rb', line 146

def ==(other)
  puts "#{self.class.name}#==(#{other.inspect})"
  if !other.is_a?(self.class)
    return false
  end

  return other.to_hash == self.to_hash
end

#[](key) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/logstash/event.rb', line 73

def [](key)
  # If the key isn't in fields and it starts with an "@" sign, get it out of data instead of fields
  if ! @data["@fields"].has_key?(key) and key.slice(0,1) == "@"
    return @data[key]
  # Exists in @fields (returns value) or doesn't start with "@" (return null)
  else
    return @data["@fields"][key]
  end
end

#[]=(key, value) ⇒ Object

TODO(sissel): the semantics of [] and []= are now different in that []= only allows you to assign to only fields (not metadata), but

allows you to read fields and metadata.

We should fix this. Metadata is really a namespace issue, anyway.



87
# File 'lib/logstash/event.rb', line 87

def []=(key, value); @data["@fields"][key] = value end

#append(event) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/logstash/event.rb', line 104

def append(event)
  self.message += "\n" + event.message 
  self.tags |= event.tags

  # Append all fields
  event.fields.each do |name, value|
    if self.fields.include?(name)
      puts "Merging field #{name}"
      self.fields[name] |= value
    else
      puts "Setting field #{name}"
      self.fields[name] = value
    end
  end # event.fields.each
end

#cancelObject



29
30
31
# File 'lib/logstash/event.rb', line 29

def cancel
  @cancelled = true
end

#cancelled?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/logstash/event.rb', line 34

def cancelled?
  return @cancelled
end

#fieldsObject

def fields



88
# File 'lib/logstash/event.rb', line 88

def fields; return @data["@fields"] end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


100
# File 'lib/logstash/event.rb', line 100

def include?(key); return @data.include?(key) end

#messageObject

def message



60
# File 'lib/logstash/event.rb', line 60

def message; @data["@message"]; end

#message=(val) ⇒ Object

def message=



61
# File 'lib/logstash/event.rb', line 61

def message=(val); @data["@message"] = val; end

#overwrite(event) ⇒ Object



95
96
97
# File 'lib/logstash/event.rb', line 95

def overwrite(event)
  @data = event.to_hash
end

#sourceObject

def source



48
# File 'lib/logstash/event.rb', line 48

def source; @data["@source"]; end

#source=(val) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/logstash/event.rb', line 49

def source=(val) 
  if val.is_a?(URI)
    @data["@source"] = val.to_s
    @data["@source_host"] = val.host
    @data["@source_path"] = val.path
  else
    @data["@source"] = val
  end
end

#sprintf(format) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/logstash/event.rb', line 136

def sprintf(format)
  result = format.gsub(/\$\{[@A-Za-z0-9_-]+\}/) do |match|
    name = match[2..-2] # trim '${' and '}'
    value = (self[name] or match)
  end
  #$stderr.puts "sprintf(#{format.inspect}) => #{result.inspect}"
  return result
end

#tagsObject

def tags



68
# File 'lib/logstash/event.rb', line 68

def tags; @data["@tags"]; end

#tags=(val) ⇒ Object

def tags=



69
# File 'lib/logstash/event.rb', line 69

def tags=(val); @data["@tags"] = val; end

#timestampObject

def timestamp



44
# File 'lib/logstash/event.rb', line 44

def timestamp; @data["@timestamp"]; end

#timestamp=(val) ⇒ Object

def timestamp=



45
# File 'lib/logstash/event.rb', line 45

def timestamp=(val); @data["@timestamp"] = val; end

#to_hashObject

def to_hash



92
# File 'lib/logstash/event.rb', line 92

def to_hash; return @data end

#to_jsonObject

def to_json



91
# File 'lib/logstash/event.rb', line 91

def to_json; return @data.to_json end

#to_sObject



39
40
41
# File 'lib/logstash/event.rb', line 39

def to_s
  return "#{timestamp} #{source}: #{message}"
end

#typeObject

def type



64
# File 'lib/logstash/event.rb', line 64

def type; @data["@type"]; end

#type=(val) ⇒ Object

def type=



65
# File 'lib/logstash/event.rb', line 65

def type=(val); @data["@type"] = val; end