Class: LogStash::Settings

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable, Util::SubstitutionVariables
Defined in:
lib/logstash/settings.rb

Direct Known Subclasses

PipelineSettings

Constant Summary

Constants included from Util::SubstitutionVariables

Util::SubstitutionVariables::SUBSTITUTION_PLACEHOLDER_REGEX

Instance Method Summary collapse

Methods included from Util::SubstitutionVariables

#deep_replace, #replace_placeholders

Constructor Details

#initializeSettings

Returns a new instance of Settings.



13
14
15
16
17
18
19
20
# File 'lib/logstash/settings.rb', line 13

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

#==(other) ⇒ Object



144
145
146
147
# File 'lib/logstash/settings.rb', line 144

def ==(other)
  return false unless other.kind_of?(::LogStash::Settings)
  self.to_hash == other.to_hash
end

#cloneObject Also known as: dup



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

def clone
  get_subset(".*")
end

#format_settingsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/logstash/settings.rb', line 92

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, file_name = "logstash.yml") ⇒ Object



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

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

#get_default(setting_name) ⇒ Object



59
60
61
# File 'lib/logstash/settings.rb', line 59

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

#get_setting(setting_name) ⇒ Object

Raises:

  • (ArgumentError)


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

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



40
41
42
43
44
45
46
47
48
# File 'lib/logstash/settings.rb', line 40

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



63
64
65
# File 'lib/logstash/settings.rb', line 63

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

#merge(hash, graceful = false) ⇒ Object



87
88
89
90
# File 'lib/logstash/settings.rb', line 87

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

#on_post_process(&block) ⇒ Object



130
131
132
133
# File 'lib/logstash/settings.rb', line 130

def on_post_process(&block)
  @post_process_callbacks ||= []
  @post_process_callbacks << block
end

#post_processObject



122
123
124
125
126
127
128
# File 'lib/logstash/settings.rb', line 122

def post_process
  if @post_process_callbacks
    @post_process_callbacks.each do |callback|
      callback.call(self)
    end
  end
end

#register(setting) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/logstash/settings.rb', line 22

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

#registered?(setting_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/logstash/settings.rb', line 30

def registered?(setting_name)
   @settings.key?(setting_name)
end

#resetObject



112
113
114
# File 'lib/logstash/settings.rb', line 112

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

#set?(setting_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



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

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



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

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

#validate_allObject



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

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