Class: LogStash::Settings

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

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



10
11
12
13
14
15
16
17
# File 'lib/logstash/settings.rb', line 10

def initialize
  @settings = {}
  # Theses settings were loaded from the yaml file
  # but we didn't find any settings to validate them,
  # lets keep them around until we do `validate_all` at that
  # time universal plugins could have added new settings.
  @transient_settings = {}
end

Instance Method Details

#cloneObject



47
48
49
# File 'lib/logstash/settings.rb', line 47

def clone
  get_subset(".*")
end

#format_settingsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/logstash/settings.rb', line 84

def format_settings
  output = []
  output << "-------- Logstash Settings (* means modified) ---------"
  @settings.each do |setting_name, setting|
    value = setting.value
    default_value = setting.default
    if default_value == value # print setting and its default value
      output << "#{setting_name}: #{value.inspect}" unless value.nil?
    elsif default_value.nil? # print setting and warn it has been set
      output << "*#{setting_name}: #{value.inspect}"
    elsif value.nil? # default setting not set by user
      output << "#{setting_name}: #{default_value.inspect}"
    else # print setting, warn it has been set, and show default value
      output << "*#{setting_name}: #{value.inspect} (default: #{default_value.inspect})"
    end
  end
  output << "--------------- Logstash Settings -------------------"
  output
end

#from_yaml(yaml_path) ⇒ Object



108
109
110
111
# File 'lib/logstash/settings.rb', line 108

def from_yaml(yaml_path)
  settings = read_yaml(::File.join(yaml_path, "logstash.yml"))
  self.merge(flatten_hash(settings), true)
end

#get_default(setting_name) ⇒ Object



51
52
53
# File 'lib/logstash/settings.rb', line 51

def get_default(setting_name)
  get_setting(setting_name).default
end

#get_setting(setting_name) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/logstash/settings.rb', line 27

def get_setting(setting_name)
  setting = @settings[setting_name]
  raise ArgumentError.new("Setting \"#{setting_name}\" hasn't been registered") if setting.nil?
  setting
end

#get_subset(setting_regexp) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/logstash/settings.rb', line 33

def get_subset(setting_regexp)
  regexp = setting_regexp.is_a?(Regexp) ? setting_regexp : Regexp.new(setting_regexp)
  settings = self.class.new
  @settings.each do |setting_name, setting|
    next unless setting_name.match(regexp)
    settings.register(setting.clone)
  end
  settings
end

#get_value(setting_name) ⇒ Object Also known as: get



55
56
57
# File 'lib/logstash/settings.rb', line 55

def get_value(setting_name)
  get_setting(setting_name).value
end

#merge(hash, graceful = false) ⇒ Object



79
80
81
82
# File 'lib/logstash/settings.rb', line 79

def merge(hash, graceful = false)
  hash.each {|key, value| set_value(key, value, graceful) }
  self
end

#register(setting) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/logstash/settings.rb', line 19

def register(setting)
  if @settings.key?(setting.name)
    raise ArgumentError.new("Setting \"#{setting.name}\" has already been registered as #{setting.inspect}")
  else
    @settings[setting.name] = setting
  end
end

#resetObject



104
105
106
# File 'lib/logstash/settings.rb', line 104

def reset
  @settings.values.each(&:reset)
end

#set?(setting_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/logstash/settings.rb', line 43

def set?(setting_name)
  get_setting(setting_name).set?
end

#set_value(setting_name, value, graceful = false) ⇒ Object Also known as: set



60
61
62
63
64
65
66
67
68
# File 'lib/logstash/settings.rb', line 60

def set_value(setting_name, value, graceful = false)
  get_setting(setting_name).set(value)
rescue ArgumentError => e
  if graceful
    @transient_settings[setting_name] = value
  else
    raise e
  end
end

#to_hashObject



71
72
73
74
75
76
77
# File 'lib/logstash/settings.rb', line 71

def to_hash
  hash = {}
  @settings.each do |name, setting|
    hash[name] = setting.value
  end
  hash
end

#validate_allObject



113
114
115
116
117
118
119
120
# File 'lib/logstash/settings.rb', line 113

def validate_all
  # lets merge the transient_settings again to see if new setting were added.
  self.merge(@transient_settings)

  @settings.each do |name, setting|
    setting.validate_value
  end
end