Module: LogStash::Config::Mixin

Included in:
LogStash::Codecs::Base, Filters::Base, Inputs::Base, Outputs::Base
Defined in:
lib/logstash/config/mixin.rb

Overview

This module is meant as a mixin to classes wishing to be configurable from config files

The idea is that you can do this:

class Foo < LogStash::Config

# Add config file settings
config "path" => ...
config "tag" => ...

# Add global flags (becomes --foo-bar)
flag "bar" => ...

end

And the config file should let you do:

foo

"path" => ...
"tag" => ...

Defined Under Namespace

Modules: DSL

Constant Summary collapse

PLUGIN_VERSION_1_0_0 =
LogStash::Util::PluginVersion.new(1, 0, 0)
PLUGIN_VERSION_0_9_0 =
LogStash::Util::PluginVersion.new(0, 9, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



35
36
37
# File 'lib/logstash/config/mixin.rb', line 35

def config
  @config
end

#original_paramsObject

Returns the value of attribute original_params.



36
37
38
# File 'lib/logstash/config/mixin.rb', line 36

def original_params
  @original_params
end

Class Method Details

.included(base) ⇒ Object

This method is called when someone does ‘include LogStash::Config’



42
43
44
45
# File 'lib/logstash/config/mixin.rb', line 42

def self.included(base)
  # Add the DSL methods to the 'base' given.
  base.extend(LogStash::Config::Mixin::DSL)
end

Instance Method Details

#config_init(params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/logstash/config/mixin.rb', line 47

def config_init(params)
  # Validation will modify the values inside params if necessary.
  # For example: converting a string to a number, etc.
  
  # Keep a copy of the original config params so that we can later
  # differentiate between explicit configuration and implicit (default)
  # configuration.
  @original_params = params.clone
  
  # store the plugin type, turns LogStash::Inputs::Base into 'input'
  @plugin_type = self.class.ancestors.find { |a| a.name =~ /::Base$/ }.config_name

  # warn about deprecated variable use
  params.each do |name, value|
    opts = self.class.get_config[name]
    if opts && opts[:deprecated]
      extra = opts[:deprecated].is_a?(String) ? opts[:deprecated] : ""
      extra.gsub!("%PLUGIN%", self.class.config_name)
      @logger.warn("You are using a deprecated config setting " +
                   "#{name.inspect} set in #{self.class.config_name}. " +
                   "Deprecated settings will continue to work, " +
                   "but are scheduled for removal from logstash " +
                   "in the future. #{extra} If you have any questions " +
                   "about this, please visit the #logstash channel " +
                   "on freenode irc.", :name => name, :plugin => self)
    end
    if opts && opts[:obsolete]
      extra = opts[:obsolete].is_a?(String) ? opts[:obsolete] : ""
      extra.gsub!("%PLUGIN%", self.class.config_name)
      raise LogStash::ConfigurationError,
        I18n.t("logstash.agent.configuration.obsolete", :name => name,
               :plugin => self.class.config_name, :extra => extra)
    end
  end

  # Set defaults from 'config :foo, :default => somevalue'
  self.class.get_config.each do |name, opts|
    next if params.include?(name.to_s)
    if opts.include?(:default) and (name.is_a?(Symbol) or name.is_a?(String))
      # default values should be cloned if possible
      # cloning prevents 
      case opts[:default]
        when FalseClass, TrueClass, NilClass, Numeric
          params[name.to_s] = opts[:default]
        else
          params[name.to_s] = opts[:default].clone
      end
    end

    # Allow plugins to override default values of config settings
    if self.class.default?(name)
      params[name.to_s] = self.class.get_default(name)
    end
  end

  if !self.class.validate(params)
    raise LogStash::ConfigurationError,
      I18n.t("logstash.agent.configuration.invalid_plugin_settings")
  end

  # We remove any config options marked as obsolete,
  # no code should be associated to them and their values should not bleed
  # to the plugin context.
  #
  # This need to be done after fetching the options from the parents classed
  params.reject! do |name, value|
    opts = self.class.get_config[name]
    opts.include?(:obsolete)
  end

  # set instance variables like '@foo'  for each config value given.
  params.each do |key, value|
    next if key[0, 1] == "@"

    # Set this key as an instance variable only if it doesn't start with an '@'
    @logger.debug("config #{self.class.name}/@#{key} = #{value.inspect}")
    instance_variable_set("@#{key}", value)
  end

  @config = params
end